I have a rather large string that I want to write to a python file object. Currently, when I try to write this string only the LAST row gets written to the file. I've tested to ensure that the variable holding the large string is infact <type 'str'>. Here is the sample content:
"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"
I want to write this to a file but everytime I do a file.write() I get only the last row. I'm using this simple file open and write procedure:
#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
I read that I should be iterating over content with a for loop; but, since content is a string and not a list/tuple, the for loop will explode to each letter and write the letter on a separate row.
Any ideas how to write this type of content to a file?
print len(content)just to verify? Can you tell us the result?contentusingrepr? Have you checked for carriage returns or other escape characters?urllib2.urlopen()to open a file, while a normalopen()function is intented to do so ?urllib2.urlopen()and it failed: ValueError: unknown url type: rada.csv For the moment, your question means nothing.