13
votes

I have a .txt file with this inside - 2.9,Gardena CA

What I'm trying to do is convert that text into a .csv (table) using a python script:

import csv
import itertools

with open('log.txt', 'r') as in_file:
    stripped = (line.strip() for line in in_file)
    lines = (line for line in stripped if line)
    grouped = itertools.izip(*[lines] * 3)
    with open('log.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('title', 'intro'))
        writer.writerows(grouped)

The output I get in the log.csv file is - title,intro,tagline

What I would want the log.csv file to show is:

title,intro
2.9,Gardena CA
5
can you more clearly state what the problem is? - harshil9968
What i'm trying to do is have the table (.csv) display: title,intro,tagline and then 1 line under, 2.9, Gardena CA - Adam Eliezerov
can you show the log.txt first few lines, just edit the code and add that. - harshil9968
But the log.txt file is an output file of a script that I can't really edit commas into. @harshil9968 - Adam Eliezerov
What harshil9968 means is: edit the code in the question above to include a sample of the output. As it stands you probably could just open the file as a csv using the space-deliminated seperator option. - Alan

5 Answers

41
votes

You need to split the line first.

import csv

with open('log.txt', 'r') as in_file:
    stripped = (line.strip() for line in in_file)
    lines = (line.split(",") for line in stripped if line)
    with open('log.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('title', 'intro'))
        writer.writerows(lines)
15
votes
import pandas as pd
df = pd.read_fwf('log.txt')
df.to_csv('log.csv')
2
votes

I suposse this is the output you need:

title,intro,tagline

2.9,Gardena,CA

It can be done with this changes to your code:

import csv
import itertools

with open('log.txt', 'r') as in_file:
    lines = in_file.read().splitlines()
    stripped = [line.replace(","," ").split() for line in lines]
    grouped = itertools.izip(*[stripped]*1)
    with open('log.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('title', 'intro', 'tagline'))
        for group in grouped:
            writer.writerows(group)
2
votes

This is how I do it:

 with open(txtfile, 'r') as infile, open(csvfile, 'w') as outfile:
        stripped = (line.strip() for line in infile)
        lines = (line.split(",") for line in stripped if line)
        writer = csv.writer(outfile)
        writer.writerows(lines)

Hope it helps!

0
votes

In python version 3.x, the intertools.izip is not operate.

The functional code for Python 3 is:

import csv
import zlib

with open('output.txt', 'r') as in_file:
    lines = in_file.read().splitlines()
    stripped = [line.replace(","," ").split() for line in lines]
    grouped = zip(*[stripped]*1)
    with open('teste.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('A', 'B', 'C', 'D'))
        for group in grouped:
            writer.writerows(group)