即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

csv文件操作

编程语言 u012550037 8℃ 0评论
  • 导入csv库
import csv
  • 写入csv文件中
with open('a.csv','w+') as csvfile:
    writer = csv.writer(csvfile,lineterminator='\n',dialect='excel')
    writer.writerow([1,2,3,4])  #注意,即使一列,也要写中括号[ ]
#注意:lineterminator='\n'的意思是除去多余的空行

This version of the csv module doesn’t support Unicode input. Also, there are currently some issues regarding ASCII NUL characters. Accordingly, all input should be UTF-8 or printable ASCII to be safe

  • 读csv文件
 with open('a.csv','r') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        print(row[0])  #打印第一列

转载请注明:CodingBlog » csv文件操作

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情