python 3.x - Print list to csv file in Python3 -
i have list this.
all_chords = [['c', 'c', 'e', 'g'], ['cm7', 'c', 'e', 'g', 'b'], ['c7', 'c', 'e', 'g', 'bb'], ['cm7', 'c', 'eb', 'g', 'bb'], ['cm7b5', 'c', 'eb', 'gb', 'bb'], ['cdim7', 'c', 'eb', 'gb', 'bbb(a)'], ['caug7', 'c', 'e', 'g#', 'bb'], ['c6', 'c', 'e', 'g', 'a'], ['cm6', 'c', 'eb', 'g', 'a'], ]
i want print out csv file, this.
c_chords.csv
c;c,e,g cm7;c,e,g,b c7;c,e,g,bb cm7;c,eb,g,bb cm7b5;c,eb,gb,bb cdim7;c,eb,gb,bbb(a) caug7;c,e,g#,bb c6;c,e,g,a cm6;c,eb,g,a
it has 2 fileds separted semicolon. (not comma)
i used csv module, this.
myfile = open('c_chords.csv','w') wr = csv.writer(myfile, quotechar=none) wr.writerows(all_chords) myfile.close()
the result is..
c,c,e,g cm7,c,e,g,b c7,c,e,g,bb cm7,c,eb,g,bb cm7b5,c,eb,gb,bb cdim7,c,eb,gb,bbb(a) caug7,c,e,g#,bb c6,c,e,g,a cm6,c,eb,g,a
should modify list? this? [['c',';', 'c', 'e', 'g'],.......]
or other brilliant ideas guys have?
thanks in advance.
you're writing 4 columns, not two, if want last list elements 1 single column, need join them first manually.
and need change delimiter if want csv semicolon separated, not quote character:
import csv all_chords = [['c', 'c', 'e', 'g'], ['cm7', 'c', 'e', 'g', 'b'], ['c7', 'c', 'e', 'g', 'bb'], ['cm7', 'c', 'eb', 'g', 'bb'], ['cm7b5', 'c', 'eb', 'gb', 'bb'], ['cdim7', 'c', 'eb', 'gb', 'bbb(a)'], ['caug7', 'c', 'e', 'g#', 'bb'], ['c6', 'c', 'e', 'g', 'a'], ['cm6', 'c', 'eb', 'g', 'a'], ] myfile = open('c_chords.csv','w') wr = csv.writer(myfile, delimiter=';') wr.writerows([c[0], ','.join(c[1:])] c in all_chords) myfile.close()
Comments
Post a Comment