将1000个文本文件转换为单个csv文件

时间:2017-11-02 10:32:06

标签: python csv

我想将多个文本文件转换为单个csv文件。文本命名为(file1.txt,file2.txt .... file1000.txt)。文本文件(file1.txt)格式如下:

Employee id: us51243
Employee name: Mark santosh
department:engineering
Age:25 

我希望输出为:

Employee id,Employee name,department,Age
us51243,Mark santosh,engineering,25//(file1.txt values)
...................................//(file2.txt values)

但是在输出中我只得到file1000.txt的值如下:

Employee id,Employee name,department,Age
us98621,Andy Gonzalez,Support & services,25

这是我的代码:

import csv
import os
for x in range(1,1001):
    filepath=os.path.normpath('C:\\Text\\file{}.txt'.format(x))
    with open(filepath) as f, open('emp.csv', 'w',newline='') as file:
        writer = csv.writer(file)
        val = zip(*[l.rstrip().split(': ') for l in f])
        writer.writerows(val)

请注意:我也只想显示标题(员工ID,员工姓名,部门,年龄)

3 个答案:

答案 0 :(得分:0)

尝试以下方法:

item-three

答案 1 :(得分:0)

您当前正在为每个新文本文件重新打开文件,导致所有内容被覆盖。此外,您可以使用CSV库来读取文本文件,方法是将分隔符指定为:并跳过任何多余的空格:

import csv
import os

header = ["Employee id", "Employee name", "department", "Age"]

with open('emp.csv', 'w', newline='') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(header)

    for x in range(1, 1001):
        filepath = os.path.normpath(r'C:\Text\file{}.txt'.format(x))

        with open(filepath, 'r', newline='') as f_text:
            csv_text = csv.reader(f_text, delimiter=':', skipinitialspace=True)
            csv_output.writerow(row[1] for row in csv_text)

答案 2 :(得分:0)

首先让我们创建两个文件:

s1 = u"""Employee id: us51243
Employee name: Mark santosh
department:engineering
Age:25"""

s2 = u"""Employee id: us51244
Employee name: Any santosh
department:engineering
Age:24"""

with open("file1.txt", "w") as f:
    f.write(s1)

with open("file2.txt", "w") as f:
    f.write(s2)

现在让我们使用pandas:

import pandas as pd

# Filelist
filelist = ["file1.txt","file2.txt"]

# Create dataframe
df = pd.DataFrame(columns=["Employee id","Employee name","department","Age","file"])


# Loop through files
for ind,file in enumerate(filelist):
    data = pd.read_csv(file, header=None, sep=":").iloc[:,1] 
    df.loc[ind] = data.tolist() + [file]

df

输出:

  Employee id Employee name   department Age       file
0     us51243  Mark santosh  engineering  25  file1.txt
1     us51243  Mark santosh  engineering  25  file2.txt