我想写清单
someList=["a=1.0,2.0,3.0","b=1.0,2.0,3.0"]
到单列,双行文件,看起来像(每行周围没有引号):
a=1.0,2.0,3.0
b=1.0,2.0,3.0
但是,如果我使用以下代码:
import csv
myfile = open("/home/user/fileName", 'wb')
wr = csv.writer(myfile)
for c in someList:
wr.writerow(c)
然后它不会给我想要的东西:
a,=,1,.,0,",",2,.,0,",",3,.,0
b,=,1,.,0,",",2,.,0,",",3,.,0
答案 0 :(得分:1)
您的行已经格式化;它们不是CSV行。
使用换行符直接将数据写入文件:
with open("/home/user/fileName", 'w') as myfile:
for line in someList:
myfile.write(line + '\n')
答案 1 :(得分:1)
由于您的数据已格式化,请使用文件对象的write
方法将整个列表写入文件。只需确保将换行符添加为行分隔符以加入行:
<强>实施强>
with open("/home/user/fileName", "w") as fin:
fin.write('\n'.join(someList))
<强>输出强>
a=1.0,2.0,3.0
b=1.0,2.0,3.0