在python3中使用csv的ploblem

时间:2016-03-03 05:53:16

标签: python csv

import csv
with open('c:/python34/test.csv', 'a', encoding='cp949') as csvfile:
    fieldnames = ['num']
     writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter=',', lineterminator='\n',)
     writer.writeheader()
     writer.writerow({'num': '04'})  
打开test.csv文件时

结果

  

4

我想在csv文件中写 04 ,但是当我打开test.csv文件时,它是 4 帮助我

1 个答案:

答案 0 :(得分:0)

尝试添加额外的'在04之前允许它不被剥离excel:

import csv
with open('c:/python34/test.csv', 'a', encoding='cp949') as csvfile:
    fieldnames = ['num']
     writer = csv.DictWriter(csvfile, fieldnames=fieldnames, delimiter=',', lineterminator='\n',)
     writer.writeheader()
     writer.writerow({'num': '\'04'}) #changed to escaped '