How to output a multiple rows csv?
Andrew Henderson
Now I parse paragraph text in some txt file with the code below:
def ParseFile(path,filename): content=open(path+filename).read() code=filename.split('.')[0] pattenstart = '' pattenend = '' for catlog in CATLOG: i = content.index(pattenstart) j = content.index(pattenend) info=content[i:j] yield (catlog,code,info) sys.stdout.write('.')and the info is a multi-line text
now I want to output a csv file like:
code info
*** **** **** ****
*** **** **** ****and I use some script to test,but only can out put a file like:
code info
*** ****
***********
**********and my test script is :
time1=time.time()
subfix='_ALL.csv'
d = defaultdict(list)
for path in [PATH1,PATH2]: print 'Parsing',path filenames = os.listdir(path) for filename in filenames: print 'Parsing',filename for item in ParseFile(path,filename): d[item[0]].append((item[1],item[2])) print
for k in d.keys(): out_file=open(DESTFILEPATH+k+subfix,'w') for code,info in sorted(set(d[k])): out_file.write(code+'\t'+info+\n') out_file.close()
print 'Done in %0.1f seconds'%(time.time()-time1)how to fix it?
01 Answer
Python has the csv module, which will let you do what you want much more easily, I suggest you give it a look.
E.g:
import csv
with open('somefile.csv', 'w') as file: output = csv.writer(file, delimiter='\t') output.writerows([ ['code', 'info'], ['****', '****'], [None, '****'], [None, '****'], [None, '****'], ['****', '****'], [None, '****'] ])Which produces:
code info
**** **** **** **** ****
**** **** ****Edit:
If your data isn't in a suitable format for this, then you simply need to change it to fit:
import csv
from itertools import izip_longest
from itertools import chain
data = [("key", ["value", "value"]), ("key", ["value", "value"])]
with open('somefile.csv', 'w') as file: output = csv.writer(file, dialect='excel-tab') output.writerows( chain.from_iterable( izip_longest([key], values) for key, values in data ) )Which produces:
key value value
key value value 13