1

我有一个相当大的字符串,我想写入一个 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循环将分解到每个字母并将字母写在单独的行上。

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

4

3 回答 3

1

您必须分析数据以查看它是否具有 wiated 格式。
你能执行这段代码吗:

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>]
于 2013-10-23T17:41:48.623 回答
0

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

于 2013-10-23T16:57:23.183 回答
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")

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

于 2013-10-23T17:07:01.920 回答