我有一个 csv 阅读器,可以将数据值拉入列表中,一旦将这些数据放入列表中,我想去除列表中的空格。我在网上查看并看到人们使用striplist()
例如
def striplist(l):
return([x.strip() for x in l])
但是,作为一个新手并尝试合并代码,我运气不佳,对此问题的指导或对我做错了什么的了解将不胜感激。我的代码如下:
import csv
import time
csvfile = open("example.csv")
filetype = csv.Sniffer().has_header(csvfile.read(1024))
csvfile.seek(0)
reader = csv.reader(csvfile,filetype)
csvreaderlist = []
csvfilecounter = 0
if filetype:
next(reader)
print("CSV file located, headers present, importing data")
time.sleep(3)
for data in reader:
csvreaderlist.append(data)
print(data)
csvfilecounter = csvfilecounter +1
summarycounter = summarycounter +1
else:
print("CSV file located, no headers found, importing data")
time.sleep(3)
for data in reader:
csvreaderlist.append(data)
csvfilecounter = csvfilecounter +1
summarycounter = summarycounter +1
print(data)
if csvfilecounter == csvfilecounter:
print(len(csvreaderlist),'Lines were successfully imported from the CSV file')
time.sleep(3)
def striplist(csvreaderlist):
return([data.strip() for data in csvreaderlist])