How to output a multiple rows csv?
18,397
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
Author by
sword
Updated on June 04, 2022Comments
-
sword 7 months
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?
-
sword over 10 yearsI have already use the csv module,but I can't output the format what I want.
-
Gareth Latty over 10 yearsYou say that, you are clearly not using the
csv
module to write your file in the code you gave. -
sword over 10 yearsI think that is not the effect I want ,I want to import the csv file to a database ,so the code and the info should be in a same row.
-
Charles Duffy over 10 years@sword from your code, it looks like you want TSV rather than CSV, but look at how the answer here is using the csv module (for that matter, the CSV module also has an alternate, tab-delimited default format, which can be selected as a whole rather than only overriding the delimiter as is done here).
-
sword over 10 yearsemmm...,I paste the code not my test csv module code,but I really test csv moudle,but output the wrong text,so I just paste the code can output the txt file.
-
sword over 10 years@Charles Duffy,thanks,I have already search stackoverflow,yes,I found some topic,I try to use that method to solve my problem,but I think isn't similar problem .
-
Gareth Latty over 10 years@sword You are not being very clear, but I have given an updated answer based on what I think you are asking.
-
sword over 10 yearsThanks Lattyware,yes,I can't describe my problem very clear,I think my problem is a problem with dict,maybe your answer just update is right,let me test.:)
-
Gareth Latty over 10 years@sword If you give all of the information - like what the data structure you want to write as CSV is, it would make this easier.
-
sword over 10 yearsthanks,Lattyware.but I am a fresh in python,and my english is not very good,I can't explain very clear to you.maybe I can paste all my script and those data I want write as vsv to you.:)
-
Gareth Latty over 10 years@sword Just give an example of your data structure. Get the data in the format you have it in your script, and add it in as an example.
-
sword over 10 years@Lattyware but the data is chinese,are you sure you want to look that?
-
Gareth Latty over 10 years@sword I'm not talking about the data itself, but the format you have it in in your python script. Is it a dict like
{key: [value, value, ...], key: [value, value, ...], ...}
? Is it a list like[key, value, value, ..., key, value, value, ...]
? Is it a list ofkey, value, value, ...
tuples? I'm talking about data structure.