使用新列将文本文件转换为csv

时间:2017-12-04 18:11:13

标签: python csv

我有一个包含姓名和号码列表的文本文件。 文本文件格式示例:

james 500

Katrina 200

kyle 600等

我想从这个文本文件中创建一个包含2列的csv文件,名称和计数,其中name包含名称,count包含数字。以下是我到目前为止所尝试的内容,

import csv

class csvTest(object):
    def __init__(self):
        self.convertToCSV()

    def convertToCSV(self):
        names = []
        with open('BoysNames.txt', 'r') as b_names, open('popular_names.csv', 'w') as out_file, open('GirlsNames.txt', 'r') as g_names:

            for b_lines in b_names:
                b_lines = b_lines.strip().split('\t')
                names.append(b_lines)
            #for g_lines in g_names:
             #   g_lines = g_lines.split('\t')
              #  names.append(g_lines)

            writer = csv.writer(out_file)
            writer.writerow(('FirstName', 'Count'))
            writer.writerows(names)

if __name__ == '__main__':
    csvTest() 

我无法正确拆分列,所有这些都进入名称。请帮忙。

1 个答案:

答案 0 :(得分:0)

熊猫这个解决方案怎么样?首先让我们创建一些示例数据:

import io

# first let's recreate your file
data1 = '''\
james 500
kyle 600'''

data2 ='''\
Katrina 200'''

file1 = io.StringIO(data1)
file2 = io.StringIO(data2)

现在我们进行实际操作:

import pandas as pd

#Let's put them in a list
#This list should in reality be changed to ["path/to/file1", path/to/file2 ...
files = [file1,file2]

# now let's read this data with pandas to a dataframe
names = ["FirstName","Count"]
df = pd.concat(pd.read_csv(f, sep=" ", header=None, names=names) for f in files)

# and output to csv:
df.to_csv("output.csv", sep=",", index=False)

结果'output.csv':

FirstName,Count
james,500
kyle,600
Katrina,200