我在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)
答案 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: