在 Python 中将竖线分隔文本文件的文件夹转换为 CSV

Convert a folder of pipe-delimited text files to CSV in Python

我有一个包含“|”的 .txt 文件文件夹而不是逗号,我正在尝试将其转换为 CSV 格式。我找到了一些应该可以工作的代码,但我一直收到错误 "iterator should return strings, not bytes (did you open the file in text mode?)"。我发现的代码没有嵌套在 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)

将打开语句更改为:

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

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

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)