Tried different ways. The closest way that may fit my need is the following code:
with open('list.csv', 'r') as reader, open('list-history.csv', 'a') as writer:
for row in reader:
writer.writerow(row)
I'm using 'a' and tried 'w' as well but no luck.
The result is no output at all.
Any suggestion, please? Thanks.
I think there should be a error with a stacktrace.
Here: writer.writerow(row)
Normally open() returns file object which doesn't have .writerow() method, normally, you should use .write(buffer) method.
Example
with open('list.csv', 'r') as reader, open('list-history.csv', 'a') as writer:
for row in reader:
writer.write(row)
For me it works well with test csv files. But it doesn't merge them, just appends content of one file to another one.
If both the csv columns have same name. Python's pandas module can help.
Example Code snippet.
import pandas as pd
df1 = pd.read_csv("csv1.csv")
df2 = pd.read_csv("csv2.csv")
df1.append(df2, ignore_index=True)
df1.to_csv("new.csv",index=False)
Related
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 have a directory containing multiple csv's that I would to read into a single dictionary. The dictionary would use the original file names as keys and the contents of the csv's as values. I don't want to use pandas because I am new to Python and want to understand these tasks first before pulling out the big guns. I would like to use DictReader for the task. Here is the code I have so far below. It works fine for one file at a time. Help is greatly appreciated.
def read_lines():
data = []
with open('vari_late_low_scores.csv', newline='') as stream:
reader = csv.reader(stream, delimiter=',', skipinitialspace=True)
for row in reader:
data.append(row)
return data
Thank you!
I have tried this over and over many times and it still will not work. All that I need to do is to read a csv file, but my program will not do it. I have tried the pandas.read_csv() and it just says it does not have that attribute:
My error message
I have also tried importing csv and reading it that way but it refuses to admit that there is a file there:
My other error message
And here is my code for pandas:
pd.read_csv('data.csv')
And my code for the rest:
with open('data.csv') as csv_file:
csv_reader = csv.DictReader(csv_file)
row = next(csv_reader)
print(row)
And last but not least, here is the proof that the file exists: Proof
Do not create modules using library names. Rename pandas.py. Try again.
df= pd.read_csv('data.csv')
I made csv file in my python code itself and going to append next data in ti it but the error is comming
io.UnsupportedOperation: not readable
I tried code is:
df.to_csv('timepass.csv', index=False)
with open(r'timepass.csv', 'a') as f:
writer = csv.reader(f)
your_list = list(writer)
print(your_list)
want to append next data and store in the same csv file. so that csv file having both previous and current data.
so please help me to find out..
Thanks in advance...
It is so simple just try this:
import pandas as pd
df = pd.read_excel("NSTT.xlsx","Sheet1") #reading Excel
print(df) #Printing data frame
df.to_excel("new.xlsx") #Writing Dataframe into New Excel file
Now here if you want to append data in the same file then use
df.to_excel("new.xlsx","a")
And no need to add in a list as you can directly access the data same as a list with data frame only you have to define the location .
Please check this.
You can use pandas in python to read csv and write csv:
import pandas as pd
df = pd.read_csv("csv file")
print(df)
Try:
with open(r'timepass.csv', 'r') as f:
reader = list(csv.reader(f))
print(reader)
Here you are opening your file as r, which means read-only and assigning the list contents to reader with list(csv.reader(f)). Your earlier code a opens the file for appending only where in the documentation is described as:
'a' opens the file for appending; any data written to the file is
automatically added to the end
and does not support the read().
And if you want to append data to the csv file from a different list, use the with open as a with the writer method.
with open('lake.csv','a') as f:
csv.writer(f,[1,2,3]) #dummy list [1,2,3]
Or directly from the pandas.DataFrame.to_csv method from your new dataframe, with header = False so as not to append headers:
df.to_csv('timepass.csv', index=False)
df_new.to_csv(r'timepass.csv', mode='a', header=False) #once you have updated your dataframe, you can directly append it to the same csv file
you can use pandas for appending two csv quickly
import pandas as pd
dataframe1=pd.read_csv("a.csv")
dataframe2=pd.read_csv("b.csv")
dataframe1=dataframe1.append(dataframe2)
dataframe1=dataframe1.reset_index(drop=True)
dataframe1.to_csv("a.csv")
I have a set of csv files I need to import into a pandas dataframe.
I have imported the filepaths as a list, FP, and I am using the following code to read the data:
for i in FP:
df = pd.read_csv(i,index_col=None, header=0).append(df)
This is working great, but unfortunately there are no datetimestamps or file identifying attributes in the files. I need to know which file each record came from.
I tried adding this line, but this just returned the filename of the final file read:
for i in FP:
df = pd.read_csv(i,index_col=None, header=0).append(df)
df['filename'] = i
I can imagine some messy multi-step solutions, but wondered if there was something more elegant I could do within my existing loop.
I'd do it this way:
df = pd.concat([pd.read_csv(f, header=None).assign(filename=f) for f in FP],
ignore_index=True)