【问题标题】:Writing multiple blank fields to csv python将多个空白字段写入csv python
【发布时间】:2015-06-17 14:01:14
【问题描述】:

我尝试将大量 CSV 导入数据库表。

我的导入模板要求一组列。如果我的输入数据只有几个所需的列(在我的情况下只有 3 个),我想将它们放在各自的列中,不满意的列留空。

例如,列表List1Column 将进入我的模板的“List1Column”等。由于此输入数据不包含我的其他列的数据,“OtherColumn”、“OtherColumn2”等,我只想让它们为空。由于我已将输入数据编译成列表(每个有效地保存一列数据),我将它们压缩成我希望它们在我的模板中的顺序。

对于空列,我必须在我的 zip 迭代中为我的模板提供一个空列表,empty,empty,empty,empty,empty,empty。有一个更好的方法吗?我可以说“空 5 次”而不是 empty,empty,empty,empty,empty,empty

无论哪种方式,我的输出都是相同的,我只知道我的方法是不好的做法,并且想清理我的代码。我提供了带有代码和输出的示例 csv 输入。

输入数据

$ cat testcsv.csv  

numbers,AthruZ,LthruN  
1,a,l  
2,b,m  
3,z,n  

代码

import csv
from itertools import izip

huckFin = open('testcsv.csv','rb')
huckCin = csv.reader(huckFin, delimiter=',', quoting=csv.QUOTE_NONE  )
csvdata = [row for row in huckCin]

List1Column = [row[0] for row in csvdata]
List2Column = [row[1] for row in csvdata]
List3Column = [row[2] for row in csvdata]

empty = ['' for row in csvdata]

with open('file.csv', 'wb') as fout:
    csvout = csv.writer(fout, delimiter = ',',
    lineterminator = '\n',
    quotechar = '"'
    )

# My template
csvout.writerow(["List1Column",
                 "OtherColumn",
                 "OtherColumn2",
                 "OtherColumn3",
                 "OtherColumn4",
                 "OtherColumn5",
                 "OtherColumn6",
                 "List2Column",
                 "List3Column"])

csvout.writerows(izip(List1Column,
                      empty,
                      empty,
                      empty,  # Is there a way
                      empty,  # to avoid this list
                      empty,  # of empty columns?
                      empty,
                      List2Column,
                      List3Column))

输出

List1Column,OtherColumn,OtherColumn2,OtherColumn3,OtherColumn4,OtherColumn5,OtherColumn6,List2Column,List3Column  
numbers,,,,,,,AthruZ,LthruN  
1,,,,,,,a,l  
2,,,,,,,b,m  
3,,,,,,,z,n  

另外,我想跳过标题行。在 perl 中我会使用:

next if $.==1  

在给定标题循环文件之前是第一行。我假设在 Python 中有一个等价物。我还在我的输出中获得了一个额外的新行......自然在 perl 我会去:

chomp($output) if eof

我还假设也有一个与之等效的 python。 $output 是我的 csvout 对象。

如果有人对如何以不同方式或更有效地执行此操作有更好的建议,请告诉我。

【问题讨论】:

  • 空列表五次仍然是空列表。你期望什么输出?
  • 我强烈建议您完成像 this 这样的教程来帮助您学习基本的 Python 控制流。
  • for _ in range(5): print empty?

标签: python list csv


【解决方案1】:

试试print str(empty) * 5

你所期望的乘法只适用于字符串。

【讨论】:

    【解决方案2】:

    您可以使用简单的while

    empty = []
    i = 0
    while i < 5:
        print empty
        i = i + 1
    

    【讨论】:

      【解决方案3】:

      使用for 循环。

      for _ in range(5):
          print listname,
      

      请注意,在print 命令中使用逗号意味着它们都将位于同一行(这似乎是您想要的)。

      【讨论】:

        【解决方案4】:
        >>> from __future__ import print_function
        >>> print(*[empty] * 5)
        [] [] [] [] []
        

        【讨论】:

          【解决方案5】:

          您可以字符串化为空,然后使用此打印字符串 x-times 选项,例如

          empty = []
          print 5*str(empty)  
          

          【讨论】:

            【解决方案6】:

            您可能想研究一下 itertools。例如:

            import itertools
            a=[]
            repeat=list(itertools.repeat(a, 10))
            print(repeat)
            

            应该给你:

            [[], [], [], [], [], [], [], [], [], []]
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-04-13
              • 2014-08-01
              • 1970-01-01
              • 1970-01-01
              • 2018-04-02
              • 1970-01-01
              • 1970-01-01
              • 2019-02-19
              相关资源
              最近更新 更多