简体   繁体   中英

How to extract two columns from a text file into a csv file using python?

I have recently started reading about file handling and I wanted to store this dataset into a CSV file making two columns, one for each X and Y. I wrote the following code for this:-

import csv
import itertools

with open('insurance_dataset.txt', 'r') as in_file:
    stripped = (line.strip() for line in in_file)
    lines= (line for line in stripped if line)
    grouped = zip(*[lines] * 1)
    with open('dataset.csv' ,'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerows(grouped)

The above code stored both the X and Y in a single row. Can anyone please suggest me modifications to my code so that I can put rows X and Y in two separate columns? Thanks in advance!

CSV stands for comma-seperated values. Therefore, in order to split between cells, all you have to do is put a comma between values.

This should help.

import csv
data = []
with open('insurance_dataset.txt', 'r') as in_file:
    for line in in_file:
        if line[0].isdigit():
            data.append(line.strip().split())     #--->Split by space

with open('dataset.csv' , 'w') as out_file:
    writer = csv.writer(out_file, delimiter =';')   #--->Semicolon Seperated
    writer.writerow(["X","Y"])     #---->HEADER
    writer.writerows(data)         #---->CONTENT

Try adding a list outside of the context manager to store the lines. Then, un-indent the second context manager. Should do the trick...

grouped_list = []

with open('insurance_dataset.txt', 'r') as in_file:
    stripped = (line.strip() for line in in_file)
    lines= (line for line in stripped if line)
    grouped_list.append(zip(*[lines] * 1))
with open('dataset.csv' ,'w') as out_file:
    writer = csv.writer(out_file)
    writer.writerows(grouped)

Or maybe you don't need a csv in order to work with the Auto Insurance in Sweden dataset:

import requests

url = 'https://www.math.muni.cz/~kolacek/docs/frvs/M7222/data/AutoInsurSweden.txt'
r = requests.get(url, allow_redirects=True)

open('AutoInsurSweden.txt', 'wb').write(r.content)

with open('AutoInsurSweden.txt') as f:
    lines_after_11 = f.readlines()[11:]


for line in lines_after_11:
    values = line.split("\t")
    print("X:"+values[0] + ", Y:"+ values[1])

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM