CSV写入无法完成

时间:2017-03-24 06:18:45

标签: python csv

无法完成CSV写入。“li_result”列表中包含数据结果,我想在csv文件中写入此数据。 这是代码

csvwrite = unicode(li_result)

csvwrite_result = csvwrite.encode('sjis')

但是dataResult.csv是空的。没有任何错误发生,所以我不知道出了什么问题。 我想用csv文件中的sjis代码编写数据。(现在我使用python2.7所以unicode用于写字母,对吗?)我删除了这些代码

fp = open(CSV_FILE_NAME_ACCOUNT, 'aw')
w = csv.writer(fp, delimiter=',')
title = 'abc'
name = 'hoge'
time = '2010-04-20 0:0:0'

u_title = unicode(title)
u_name = unicode(name)
u_time = unicode(time)

s_title = u_title.encode('sjis')
s_name = u_name.encode('sjis')
s_time = u_time.encode('sjis')

list = [s_title, s_name, s_time]

w.writerow(list)

仍然没有写。 我该怎么做才能解决这个问题?

示例代码

id a1 a2
01 _1 _1
01 _1 _2
01 _1 _3
01 _2 _1
01 _2 _2
01 _2 _3

1 个答案:

答案 0 :(得分:0)

import csv

fp = open('system path to your file on which data to read', 'w')
w = csv.writer(fp, delimiter=',')
title = 'abc'
name = 'hoge'
time = '2010-04-20 0:0:0'

list = [title, name, time]

w.writerow(list)