I have a problem with a csv :
CSV image problem
I have HTML code in a csv but in some rows, the code broke the csv format.
I would like to know if somebody know how to solve this problem with python.
I would to fix the csv to get this format.
csv with format
I try to use with open and pandas to create a new csv
import csv
with open('csv_problem.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
Related
I have this code to scrape the results from google. If I have a list of terms I need to search in Excel/Csv format, how can I write the code to
After import the excel file, search each row values and print out the results for that row.
Repeat for the next row value in the Excel file.
Here's my code. Please help with any solution you can think of
For example my Excel file just have 1 column and 3 values as below:
List to search
Defuse
Commercial
Ecommerce
from ecommercetools import seo
import csv
import pandas as pd
searching = input('What do you want to search?')
results = seo.get_serps(searching)
df = pd.DataFrame(results.head(20)) # Convert result into data frame.
df.to_csv("ScanOutput.csv",mode="a")
Thank you
I tried with several module but stuck somehow. Any help would be appreciated
If this is the content of your .csv file called file.csv:
a,b,c
1,2,3
k,l,m
then you can read it and loop row by row like so:
import csv
# read file.csv and print each row
with open('file.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
This answer doesn't use pandas (but csv which is simpler) which I think is ok until you don't have gigabytes of data or the data is very complex
My company recently purchased a machine and I'm trying to find a way to store its data in a our data base, but first I need to clean up the CSV or Select Certain cells to write into a new csv. I'm currently Using Python 3.9XX
I need to extract the fallowing items for from this file. Serial number(Highlighted Yellow),Start time,End time, Pass-step,fail-steps, and Test Results.
If I can manage to select one cell it will try to do the rest on my own but im currently stuck trying to select the serial number and then writing into a new csv .
DATA FROM CSV
import csv
# read CSV
csvFile = r"C:\Users\Hunter\Documents\Programing\Python\Measu Dev\11.csv"
f=open(csvFile,'rt')
myReader = csv.reader(f)
Headers = ['SerialNo','PartNo','Startime','Endtime','TabPassed','TabFailed','TestResult']
Serialno = []
with open( 'Processed.csv', 'w', encoding='utf-8', newline='') as csvfile:
writer=csv.writer(csvfile)
writer.writerow(Headers)
writer.writerow(SerialNo)
RESULT
This is my ending result, I want to be able to store the serial number under its header 'SerialNo' but nothing seems to work on my end. I'm pretty new to this any help will be appreciated it.
thank you guys.
I am using pandas to load, modify and save csv files. Actually pandas and its dataframe functionality is only a workaround for me, as I do not need this. I only need it, because I have to modify my csv file.
I need to remove certain lines (rows). My current code is as follows:
import pandas as pd
test=pd.read_csv('myfile.csv', sep=';', skiprows=[0,1,3,4,6])
test.to_csv('myoutputfile.csv', index=False, sep=';')
I would like to maniuplate the csv file directly. I know that with import csv and next(reader) I could for example skip the first row. However, I need to skip these specific rows: skiprows=[0,1,3,4,6] and I don't know how to do this. So is there a way to modify the csv files without using pandas and save the changes?
generally, if you have a list of rows to skip, you can use something like this:
import csv
skiprows = (0,1,3,4,6)
with open('myfile.csv') as fp_in:
reader = csv.reader(fp_in, delimiter=';')
rows = [row for i, row in enumerate(reader) if i not in skiprows]
with open('myoutputfile.csv', 'w', newline='') as fp_out:
writer = csv.writer(fp_out)
writer.writerows(rows)
I want to nest JSON from flatten CSV file, how to create a parent category using all the subcategories in flatten CSV file. Please check out the format of data in given image below.
Image of the formats are in here
Any help would be appreciated!
You can read about csv module here:
https://docs.python.org/3/library/csv.html
There is a class there called DictReader which takes a CSV file and puts it in a dictionary, and from there the way to json is easy.
The following example is presented there:
import csv
with open('names.csv', newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row['first_name'], row['last_name'])
Output:
Eric Idle John Cleese
Hi I would like to get stock prices data in an csv-file. I can read the stock prices but can't see them as a csv file.
I have searched for an soultion but couldn't find it.
from alpha_vantage.timeseries import TimeSeries
#my api server
ts = TimeSeries(key='YOUR_API_KEY',output_format='csv')
data = ts.get_intraday(symbol='AAPL',interval='1min', outputsize='compact')
print(data)
#(<_csv.reader object at 0x0F461370>, None)
How can I save the data now as an csv-file to work with it?
Niklas
You have a csv reader object. You can either iterate it or use list
Ex:
print(list(data))
or
for row in data:
print(row)
To write CSV to file use:
import csv
with open("outfile.csv", "w") as outfile:
writer = csv.writer(outfile)
writer.writerows(data)
You can use Python Pandas
Pandas
It has many features, like the ones you need, reading from dataframe or exporting to CSV. You can do much more things using the Pandas DataFrame.
DataFrame.to_csv