python - Writing a table in a text file -
i trying write condensed list of music tracks,times have been played , artist text file in tabular form.
however tracks in list form, i.e.
08:04:10 current track playing = skinny genes - eliza doolittle 08:07:09 current track playing = keep on walking - scouting girls 08:10:45 current track playing = thinking of me - olly murs 08:14:01 current track playing = hangin' on string - loose ends 08:17:34 current track playing = again - janet jackson
throughout text file.
my code looks this
open('log','w').writelines([(line[:33]+line[42:]) line in open(fl) if "current track playing" in line])
any ideas on how put data table rather list?
use csv.writer
write csv-style data files.
example:
import csv import re pattern = re.compile('([0-9]*:[0-9]*:[0-9]*) current track playing = ([^-]*?) - ([^-]*)$') csv_file = open('music.csv', 'wb') csv_writer = csv.writer(csv_file, delimiter=';', quotechar='"', quoting=csv.quote_minimal) open('music.log', 'r') music_log: line in music_log: timestamp, song, artist = pattern.match(line.strip()).groups() csv_writer.writerow([timestamp, song, artist]) csv_file.close()
(this assumes you've done stripping of numerical ids log asked in question).
output:
08:04:10;skinny genes;eliza doolittle 08:07:09;keep on walking;scouting girls 08:10:45;thinking of me;olly murs 08:14:01;hangin' on string;loose ends 08:17:34;together again;janet jackson
Comments
Post a Comment