我有一个相当大的字符串,我想写入一个 python 文件对象。目前,当我尝试写入此字符串时,只有最后一行被写入文件。我已经测试以确保保存大字符串的变量是 infact <type 'str'>
。以下是示例内容:
"0008788014065251","Rush Running - Bentonville","1030643167","5,788.00","11.55","5.77"
"0008788014065271","Rush Running - Fayetteville","1030643159","1,577.00","3.16","1.58"
"0008788014108297","Snow Ball Express","2423373737","11.00","0.04","0.02"
"0008788014108354","Snow Ball Express","2423378892","1,421.00","5.69","2.84"
"0008788014108374","Snow Ball Express","2423378959","59.00","0.24","0.12"
"0008788014110860","Sound Master","2423477231","135.00","0.54","0.27"
"0008788014074301","The Baby's Room","1030669816","6,912.00","13.82","6.91"
"0008788014110760","The Reserve","2423470822","715.00","2.86","1.43"
"0008788014077339","Tool Town LLC","1171354079","438.00","0.88","0.44"
我想把它写到一个文件中,但每次我做的时候file.write()
我只得到最后一行。我正在使用这个简单的文件打开和写入过程:
#link is a url to a csv file
export = urllib2.urlopen( link )
content = export.read()
with open("somefile.csv", "w") as file:
try:
file.write( content )
except Exception, e:
raise e
我读到我应该用循环进行content
迭代for
;但是,由于content
是字符串而不是列表/元组,因此for
循环将分解到每个字母并将字母写在单独的行上。
任何想法如何将此类内容写入文件?