Python 3.x 从列表中删除空白
我正在从CSV文件中读取数据,并将行添加到列表中。在我的脚本中有一些空白导致了问题。我需要从列表中删除那些我设法删除的空白。但是,有人能告诉我这是不是正确的方法吗Python 3.x 从列表中删除空白,python-3.x,Python 3.x,我正在从CSV文件中读取数据,并将行添加到列表中。在我的脚本中有一些空白导致了问题。我需要从列表中删除那些我设法删除的空白。但是,有人能告诉我这是不是正确的方法吗 ip_list = [] with open('name.csv') as open_file: read_file = csv.DictReader(open_file) for read_rows in read_file: ip_list.append(read_rows['column1'])
ip_list = []
with open('name.csv') as open_file:
read_file = csv.DictReader(open_file)
for read_rows in read_file:
ip_list.append(read_rows['column1'])
ip_list = list(filter(None, ip_list))
print(ip_list)
还是功能更好 下面是一个读取csv文件并存储在列表中的好方法
L=[] #Create an empty list for the main array
for line in open('log.csv'): #Open the file and read all the lines
x=line.rstrip() #Strip the \n from each line
L.append(x.split(',')) #Split each line into a list and add it to the
#Multidimensional array
print(L)
例如,此csv文件将生成如下输出
This is the first line, Line1
This is the second line, Line2
This is the third line, Line3
这个,
因为csv意味着逗号分隔值您可以基于逗号进行筛选我想阅读特定列,但它会附加整个内容,包括空格。函数会有帮助吗?我无法运行此函数。def remove_spaces(ip_list):ip_list=list(filter(None,ip_list))返回ip_list
List = [('This is the first line', 'Line1'),
('This is the second line', 'Line2'),
('This is the third line', 'Line3')]