使用CSV Sniffer确定分隔符但不能处理多个文件

时间:2018-04-11 09:01:07

标签: python python-3.x

我在CSV阅读器中使用Sniffer类来确定CSV文件中的分隔符,它适用于单个文件,但如果我添加循环并将其指向具有相同CSV的文件夹,它抛出了这个错误:

File "delimiter.py", line 17, in read_csv_delimit
reader = csv.reader(csvfile, dialect)
TypeError: "delimiter" must be a 1-character string

脚本如下所示:

#!/usr/local/bin/python3

import csv
import os

def read_csv_delimit(file_dir, csv_file):
    # Initialise list
    file_csv = []
    # Open csv & check delimiter
    with open(file_dir + "/" + csv_file, newline='', encoding = "ISO-8859-1") as csvfile:
        dialect = csv.Sniffer().sniff(csvfile.read(1024))
        csvfile.seek(0)
        reader = csv.reader(csvfile, dialect)
        for item in reader:
            file_csv.append(item[0])
        #del file_csv[0]
        return file_csv

def split_path(full_path):
    #path = path.rstrip(os.sep)
    head, tail = os.path.split(full_path)
    return (head, tail)


machine_dir = input("Drop the folder here: ")

# Get list of machine csv
machines = os.listdir(machine_dir)

for machine in machines:
    print(machine)
    #file_dir, csv_file = split_path(csv_file)
    machine_list = read_csv_delimit(machine_dir, machine)
    print(machine_list)

1 个答案:

答案 0 :(得分:2)

鉴于跟踪,您的脚本似乎确实选择了非CSV文件。您可以使用glob模块微调搜索模式,只选择您想要的文件,但即使是简单的扩展查找也应该足够了:

target = input("Drop the folder here: ")

machine_list = [read_csv_delimit(target, m) for m in os.listdir(target) if m[-4:] == ".csv"]
print(machine_list)

强烈建议您检查输入的目录有效性,即使使用最简单的os.path.isdir(target)执行也是如此。

我还建议您使用os.path工具在read_csv_delimit()函数中构建路径,例如:

with open(os.path.join(file_dir, csv_file), newline='', encoding = "ISO-8859-1") as csvfile: