问题描述
我有一个输入csv,其中包含要尝试进入列表的可变列数.我的测试是解析输入的csv,并在csv列周围创建一个包含额外元素的列表.我想看到的是仅包含csv元素的列表,而且我也得到了空的带引号的元素.我需要一些帮助,以了解缺少的CSV阅读器的哪些选项.
I have an input csv with a variable number of columns I'm trying to pull into a list. My test is parsing the input csv and creating a list with extra elements around the csv columns. What I would like to see is a list that contains only the csv elements, and I'm getting empty quoted elements as well. I need some help understanding what options to the csv reader I'm missing.
示例输出:
$ python cond.py
opening conditions file conditions.lst
parser 0 input line:
"string1:", "string1b,string1c,"
output list elements:
['string1:']
['', '']
['']
['string1b,string1c,']
[]
parser 1 input line:
"stringa:", "stringb,stringc,"
output list elements:
['stringa:']
['', '']
['']
['stringb,stringc,']
[]
parser 2 input line:
"string3:", "string3next=abc", "string3b","string3c:", "string3d"
output list elements:
['string3:']
['', '']
['']
['string3next=abc']
['', '']
['']
['string3b']
['', '']
['string3c:']
['', '']
['']
['string3d']
[]
输入文件:
$ cat conditions.lst
"string1:", "string1b,string1c,"
"stringa:", "stringb,stringc,"
"string3:", "string3next=abc", "string3b","string3c:", "string3d"
Python cond.py文件:
Python cond.py file:
$ cat cond.py
from __future__ import print_function
#from csv import reader
import re
import sys
import csv
# variables
conditionsFile = "conditions.lst"
parserConditions = []
numOfParsers = 0
print("opening conditions file ", conditionsFile)
with open(conditionsFile, "r") as cf:
for line in cf:
print("parser ", numOfParsers, " input line:")
print(line.strip())
r = csv.reader(line, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True)
print("output list elements:")
for cline in r:
print(cline)
numOfParsers = numOfParsers + 1
print("")
print("total number of parsers: ", numOfParsers)
更新:在@Jean-FrançoisFabre的帮助下,我尚未解决根本原因,但有了解决方法-我将csv元素放入列表中,然后删除了空白元素.
Update:Using help from @Jean-FrançoisFabre I haven't solved the root reason but have got a workaround - I put the csv elements into a list then remove the blank elements.
for cline in r:
conditions.extend(cline)
conditions = filter(None, conditions)
print(conditions)
推荐答案
csv.reader
接受类似于文件的对象,而不是字符串...因此,它奇怪地遍历了行的字符而不是文件的行.您只需要:
csv.reader
takes a file-like object not a string...so it is iterating strangely over the characters of a line instead of the lines of a file. You just need:
from __future__ import print_function
import csv
with open('conditions.lst','rb') as cf:
r = csv.reader(cf,skipinitialspace=True)
for line in r:
print(line)
输出:
['string1:', 'string1b,string1c,']
['stringa:', 'stringb,stringc,']
['string3:', 'string3next=abc', 'string3b', 'string3c:', 'string3d']
这篇关于csv阅读器输出多余的空白项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!