为了账号安全,请及时绑定邮箱和手机立即绑定

如何用Python读写CSV文件?

如何用Python读写CSV文件?

慕的地10843 2019-06-15 18:09:48
如何用Python读写CSV文件?我有一份文件example.csv有内容1,"A towel,",1.042," it says, ",2.01337,is about the most ,-10,massively useful thing ,123-2,an interstellar hitchhiker can have.,3我怎么看这个?example.csv和Python?同样,如果我有data = [(1, "A towel,", 1.0),         (42, " it says, ", 2.0),         (1337, "is about the most ", -1),         (0, "massively useful thing ", 123),         (-2, "an interstellar hitchhiker can have.", 3)]我怎么写data到带有Python的CSV文件中?
查看完整描述

3 回答

?
桃花长相依

TA贡献1860条经验 获得超8个赞

编写CSV文件

首先,您需要导入CSV

例如:

import csvwith open('eggs.csv', 'wb') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                        quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])


查看完整回答
反对 回复 2019-06-15
?
潇潇雨雨

TA贡献1833条经验 获得超4个赞

import csvwith open(fileLocation+'example.csv',newline='') as File: #the csv file is stored in a File object

    reader=csv.reader(File)       #csv.reader is used to read a file
    for row in reader:
        print(row)


查看完整回答
反对 回复 2019-06-15
  • 3 回答
  • 0 关注
  • 529 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信