在Python中将管道分隔的文本文件的文件夹转换为CSV

时间:2019-01-04 14:36:02

标签: python csv file-conversion

我有一个.txt文件文件夹,其中包含“ |”而不是逗号,我正在尝试将其转换为CSV格式。我找到了一些应该可以使用的代码,但是我不断收到错误消息:“迭代器应返回字符串,而不是字节(您是否以文本模式打开文件?)”。我发现的代码没有嵌套在for循环中,这可能是问题吗?

代码:

import csv
import os

folder_path= r'C:\Users\%user%\Documents\data\Dataset'
txt_files = os.listdir(folder_path)

to_csv = []

for file in range(0, len(txt_files)):
    path_name = os.path.abspath(os.path.join(folder_path, txt_files[file]))
    to_csv.append(path_name)

for file in to_csv:
    with open(file, "rb") as f:
        with_pipes = csv.reader(f, delimiter='|')
        wo_pipes = list(with_pipes)

2 个答案:

答案 0 :(得分:2)

将打开的语句更改为:

with open(file, "r", encoding="utf-8") as f:

这将以文本模式(而不是二进制模式)打开文件,并且编码允许您读取非ASCII内容

答案 1 :(得分:0)

with open(output_file_name, 'w') as f_out:
    for line in source_lines:
        # get the count of delimiters in a line
        pipe_cnt = line.count('|')
        # replacing the delimiters in the line bases on count from previous step
        line = line.replace('|', ',', pipe_cnt)
        f_out.write(line)