I want to append dictionary data to an existing csv, without writing again headers.
I tried every thing from this issue: append new row to old csv file python
I tried pandas and csv solutions.
try:
if os.path.isfile(csv_filepath) is False:
df = pandas.DataFrame.from_dict(dict_data, orient="index")
df.to_csv(csv_filepath)
else:
list_values = []
list_keys = []
for key, value in dict_data.items():
list_values.append(value)
list_keys.append(key)
with open(csv_filepath, 'a', newline='') as fd:
writer = csv.DictWriter(fd, fieldnames=list_keys)
writer.writerow(dict_data)
except Exception as e:
raise e
I also tried
with open(csv_filtepath, 'a') as f:
writer = csv.writer(f)
writer.writerow(dict_data)
but both erase and write the new data without appending.
I also tried to to load the both csv in dataframes then append the second to the first but it added headers two times
EDIT:
For exemple, I first create a csv with: {'toto': 1, 'tata': 2, 'titi': 3}
Then I want to append the created csv {'toto': 2, 'tata': 1, 'titi': 5}
After this operation, I want to have 1 and 2 for in toto column, 2 and 1 in the tata column and 3 and 5 in the titi column.
EDIT 2
I tried this:
df1 = pd.read_csv(csv_filepath)
df2 = pd.DataFrame(dict_data)
df3 = pd.concat([df1, df2], axis=0)
df3.to_csv(csv_filepath)
but I have an error message : 'Error tokenizing data. C error: Expected 2 fields in line 20, saw 9'
EDIT 3
list_keys = []
for key, value in dict_data.items():
list_keys.append(key)
with open(csv_filepath, 'a', newline='') as fd:
writer = csv.DictWriter(fd, fieldnames=list_keys)
writer.writerow([data['toto'], data['tata'], data['titi']])
And I have this error message: 'list' object has no attribute 'keys'
I want to add to an existing csv file, data from a dictionary with same keys.