如何使用python从文本文件中将两列提取到csv文件中?

时间:2018-05-07 12:19:30

标签: python file csv

我最近开始阅读有关文件处理的内容,我想将this数据集存储到一个CSV文件中,每个列有一列,每个X和Y一个。我为此编写了以下代码: -

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)

以上代码将X和Y存储在一行中。任何人都可以建议我修改我的代码,以便我可以将行X和Y放在两个单独的列中?提前谢谢!

4 个答案:

答案 0 :(得分:0)

CSV代表逗号分隔值。 因此,为了在单元格之间进行分割,您只需在值之间加上逗号即可。

答案 1 :(得分:0)

这应该有所帮助。

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

答案 2 :(得分:0)

尝试在上下文管理器之外添加一个列表来存储这些行。然后,取消缩进第二个上下文管理器。应该做的伎俩...

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)

答案 3 :(得分:0)

或者您可能不需要csv才能使用Auto Insurance in Sweden数据集:

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])