Reputation: 87
As per this tutorial, I want to write to a csv file. What would the equivalent code be for the following block?
writer = pd.ExcelWriter("my-diff-2.xlsx")
diff_output.to_excel(writer,"changed")
removed_accounts.to_excel(writer,"removed",index=False,columns=["account number", "name","street","city","state","postal code"])
added_accounts.to_excel(writer,"added",index=False,columns=["account number", "name","street","city","state","postal code"])
writer.save()
Upvotes: 2
Views: 4174
Reputation: 863116
This block write 3 DataFrames to 3 sheets in one excel file. But csv file has no sheets. So simplier solution is write each DataFrame
separately to 3 csv
:
diff_output.to_csv('file1.csv', index=False)
removed_accounts.to_csv('file2.csv', index=False)
added_accounts.to_csv('file3.csv', index=False)
Upvotes: 3
Reputation: 788
import csv
with open("my-diff-2.csv","w+") as f:
writes = csv.writer(f)
writes.writerow(["account number", "name","street","city","state","postal code"]) #this is header
for lines in [["a","b"]]: # here it should be list of list
writes.writerow(lines) # writing the data in row-wise
Upvotes: 0
Reputation: 140
You can use
import csv
myData = [["first_name", "second_name", "Grade"],
['Alex', 'Brian', 'A'],
['Tom', 'Smith', 'B']]
myFile = open('example2.csv', 'w')
with myFile:
writer = csv.writer(myFile)
writer.writerows(myData)
print("Writing complete")
Upvotes: 0