只有最后一行从python中的大字符串写入文件

时间:2013-10-23 16:50:01

标签: python string file-io

我有一个相当大的字符串,我想写一个python文件对象。目前,当我尝试编写此字符串时,只有LAST行被写入文件。我已经测试过以确保保存大字符串的变量是<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循环将爆炸到每个字母并将该字母写在一个单独的行上。

如何将此类内容写入文件?

3 个答案:

答案 0 :(得分:1)

您必须分析数据以查看其是否具有相关格式 你能执行这段代码吗?

import urllib2

export = urllib2.urlopen( link )
content = export.read()

splt = content.splitlines(True) # True keeps the newlines
print 'len of splt : %d' % len(splt)
print [len(line.split(',')) for line in splt]

import re
print [re.match('"\d+",',line) for line in splt]

从SADMICROWAVE更新 以下是您要求我执行的步骤中的内容:

len of splt : 48
[6, 8, 6, 6, 6, 6, 6, 6, 7, 6, 6, 7, 6, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 6, 7, 6, 7, 6, 6, 6, 6, 6, 7, 7, 7, 7, 6, 7, 6, 6, 7, 6, 6]
[None, <_sre.SRE_Match object at 0x1f578b8>, <_sre.SRE_Match object at 0x1f57bf8>, <_sre.SRE_Match object at 0x1f57e68>, <_sre.SRE_Match object at 0x1f57ed0>, <_sre.SRE_Match object at 0x1f57f38>, <_sre.SRE_Match object at 0x216e030>, <_sre.SRE_Match object at 0x216e098>, <_sre.SRE_Match object at 0x216e100>, <_sre.SRE_Match object at 0x216e168>, <_sre.SRE_Match object at 0x216e1d0>, <_sre.SRE_Match object at 0x216e238>, <_sre.SRE_Match object at 0x216e2a0>, <_sre.SRE_Match object at 0x216e308>, <_sre.SRE_Match object at 0x216e370>, <_sre.SRE_Match object at 0x216e3d8>, <_sre.SRE_Match object at 0x216e440>, <_sre.SRE_Match object at 0x216e4a8>, <_sre.SRE_Match object at 0x216e510>, <_sre.SRE_Match object at 0x216e578>, <_sre.SRE_Match object at 0x216e5e0>, <_sre.SRE_Match object at 0x216e648>, <_sre.SRE_Match object at 0x216e6b0>, <_sre.SRE_Match object at 0x216e718>, <_sre.SRE_Match object at 0x216e780>, <_sre.SRE_Match object at 0x216e7e8>, <_sre.SRE_Match object at 0x216e850>, <_sre.SRE_Match object at 0x216e8b8>, <_sre.SRE_Match object at 0x216e920>, <_sre.SRE_Match object at 0x216e988>, <_sre.SRE_Match object at 0x216e9f0>, <_sre.SRE_Match object at 0x216ea58>, <_sre.SRE_Match object at 0x216eac0>, <_sre.SRE_Match object at 0x216eb28>, <_sre.SRE_Match object at 0x216eb90>, <_sre.SRE_Match object at 0x216ebf8>, <_sre.SRE_Match object at 0x216ec60>, <_sre.SRE_Match object at 0x216ecc8>, <_sre.SRE_Match object at 0x216ed30>, <_sre.SRE_Match object at 0x216ed98>, <_sre.SRE_Match object at 0x216ee00>, <_sre.SRE_Match object at 0x216ee68>, <_sre.SRE_Match object at 0x216eed0>, <_sre.SRE_Match object at 0x216ef38>, <_sre.SRE_Match object at 0x216f030>, <_sre.SRE_Match object at 0x216f098>, <_sre.SRE_Match object at 0x216f100>, <_sre.SRE_Match object at 0x216f168>]

答案 1 :(得分:0)

您可以尝试使用readlineswritelines代替,但它应该是相同的......也许如果行结尾编码不同(mac / unix / win),这个可以产生正确的结果。

答案 2 :(得分:0)

这是你在找什么?

export = urllib2.urlopen( link )
content = export.read()
content_list = content.split("\n")

with open("somefile.csv", "a") as f:      # note the "a" for (a)ppending
     for line in content.list:
         f.write(line + "\n")

据我了解,你遇到的唯一问题是因为你是在迭代字符串而不是逐行迭代?