csv阅读器输出多余的空白项目

时间:2018-12-05 20:22:26

标签: python python-2.7 csv

我有一个输入csv,其中包含要尝试进入列表的可变列数。我的测试是解析输入的csv并创建一个列表,其中csv列周围有额外的元素。我想看到的是仅包含csv元素的列表,并且我也得到了空的带引号的元素。我需要一些帮助,以了解缺少的CSV阅读器的哪些选项。

示例输出:

$ 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文件:

$ 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元素放入列表中,然后删除了空白元素。

for cline in  r:
  conditions.extend(cline)

conditions = filter(None, conditions)
print(conditions)

1 个答案:

答案 0 :(得分:0)

csv.reader接受的是类似文件的对象而不是字符串...因此它在一行的字符而不是文件的行上进行奇怪的迭代。您只需要:

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']