我打開和在同一時間從文件夾中讀取一個.csv文件並打印出來如下閱讀的.csv文件時:如何擺脫[「」]在Python
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', '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'(https://docs.python.org/3/library/csv.html#csv.reader)模塊中的標準庫讀取的CSV文件 – 2017-10-29 02:14:48
以及它依賴於數據你的文件,你可以請添加你的文件中的數據的例子。 – scharette