我正在打开并一次从文件夹中读取一个.csv文件并将其打印出来,如下所示:
ownerfiles = os.listdir(filepath)
for ownerfile in ownerfiles:
if ownerfile.endswith(".csv"):
eachfile = (filepath + ownerfile) #loops over each file in ownerfiles
with open (eachfile, 'r', encoding="UTF-8") as input_file:
next(input_file)
print(eachfile)
for idx, line in enumerate(input_file.readlines()) :
line = line.strip().split(",")
print(line)
然而,当我print(line)
时,文件打印如下:
/Users/Sulz/Desktop/MSBA/Applied Data Analytics/Test_File/ownerfile_138.csv
['']
['2010-01-01 11:28:35', '16', '54', '59', '0000000040400', 'O.Coffee Hot Small', 'I', ' ', ' ', '14', '1', '0', '0.3241', '1.4900', '1.4900', '1.4900', '0.0000', '1', '0', '0', '0', '0.0000', '0.0000', '1', '44', '0', '0.00000000', '1', '0', '0', '0.0000', '0', '0', '', '0', '5', '0', '0', '0', '0', 'NULL', '0', 'NULL', '', '0', '20436', '1', '0', '0', '1']
如何在所有数据列表之前删除['']
修改
我现在尝试用.csv模块读取它:
ownerfiles = os.listdir(filepath)
for ownerfile in ownerfiles:
if ownerfile.endswith(".csv"):
eachfile = (filepath + ownerfile) #loops over each file in ownerfiles
with open (eachfile, 'r', encoding="UTF-8") as input_file:
next(input_file)
reader = csv.reader(input_file, delimiter=',', quotechar='|')
for row in reader :
print(row)
然而,它仍然打印输出:
[] [' 2010-01-01 11:28:35',' 16',' 54',' 59' ,' 0000000040400',' O.Coffee Hot Small',' I',' ',' ',' 14',' 1',' 0',' 0.3241',' 1.4900&#39 ;,' 1.4900',' 1.4900',' 0.0000',' 1',' 0', ' 0',' 0',' 0.0000',' 0.0000',' 1',&#39 ; 44',' 0',' 0.00000000',' 1',' 0',' 0&#39 ;,' 0.0000',' 0',' 0','',' 0',&# 39; 5',' 0',' 0',' 0',' 0',' NULL&#39 ;,' 0',' NULL','',' 0',' 20436',&#39 ; 1',' 0',' 0',' 1']
答案 0 :(得分:1)
这就是打印Python的列表语法。您正在分隔生成列表的逗号上的每一行。如果你在拆分之前打印线,你可能会得到你想要的东西:
line = line.strip()
print(line)
line = line.split(",")
顺便说一下,如果你不知道,Python有built in CSV module用于读写csv文件。
编辑:抱歉,我误解了你的问题。将其添加到readlines循环的开头:
line = line.strip()
if not line:
continue