Convert txt to csv python script -
i have .txt file inside - 2.9,gardena ca
what i'm trying convert text .csv (table) using python script:
import csv import itertools open('log.txt', 'r') in_file: stripped = (line.strip() line in in_file) lines = (line line in stripped if line) grouped = itertools.izip(*[lines] * 3) open('log.csv', 'w') out_file: writer = csv.writer(out_file) writer.writerow(('title', 'intro')) writer.writerows(grouped)
the output in log.csv file - title,intro,tagline
what want log.csv file show is:
title,intro 2.9,gardena ca
you need split line first.
import csv open('log.txt', 'r') in_file: stripped = (line.strip() line in in_file) lines = (line.split(",") line in stripped if line) open('log.csv', 'w') out_file: writer = csv.writer(out_file) writer.writerow(('title', 'intro')) writer.writerows(lines)
Comments
Post a Comment