I printed a dictionary final_merged
to a csv file and the output looks like this:
2015-08-02 07:00 ['1.00', '0.68', '0.68', '0.00', '9', '19.1', '19.1', '19.1']
I'd prefer if it would look like this:
2015-08-02 07:00 1.00 0.68 0.68 0.00 9 19.1 19.1 19.1
so I need to get rid of all '
[
and ]
is there an easy way to convert the dictionary?
so far I tried this with two for loops
for value in final_merged.items():
line = ''
for values in value[1]:
line= line+ str(values)+ '\t'
value[1]= line
but I've got problems with the str command
print (values[1]) shows:
['1.00', '0.68', '0.68', '0.00', '9', '19.1', '19.1', '19.1']
The Dictionary looks like:
{'2015-08-02 07:00': ['1.00', '0.68', '0.68', '0.00', '9', '19.1', '19.1', '19.1']}
I'd like it to look like this
2015-08-02 07:00 1.00 0.68 0.68 0.00 9 19.1 19.1 19.1
Use str.join
. You can pass it a list and then it will make a string with all the elements joined into one string. But you can also have a specific string you want inserted between each of them, like '\\t'
.
line = line + '\t'.join(values) + '\t'
Which should give you the correct output.
thx i solved it: in te writer at the end of the code I simply entered:
w=csv.writer(f,delimiter='\t')
w.writerow(header)
for key, values in sorted(final.merged()):
w.writerow([key] + values)
before I had only had
w.writerows(sorted(merge_d.items();))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.