根据this tutorial,我想写一个csv文件。以下块的等效代码是什么?
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()
答案 0 :(得分:1)
此块在一个excel文件中将3个DataFrame写入3张。但是csv文件没有工作表。因此,更简单的解决方案是将每个DataFrame
分别写入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)
答案 1 :(得分:0)
您可以使用
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")
答案 2 :(得分:0)
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