简体   繁体   中英

ploblem using csv in python3

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'})  

result when I open test.csv file

4

I want write 04 in csv file , but it is 4 when I open test.csv file help me

try this to add an extra ' in front of the 04 to allow it to not be stripped in 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 '

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM