【问题标题】:Python 3 script for exporting a csv用于导出 csv 的 Python 3 脚本
【发布时间】:2016-12-21 08:09:44
【问题描述】:

您好,我有一个 csv 文件,其中有两列,一列是数字,另一列是字母,格式如下:

1234  k

343   o

5687  uiuuo

我要做的就是用以前的值填充空白行。我已经编写了将我的工作保存在新 csv 中的代码,但我收到一条错误消息:

b = w[1]
IndexError: list index out of range

这是我的代码

import csv
with open('col.csv', 'r') as f:
    reader = csv.reader(f)
    my_list = list(reader)
#print my_list[1]
#x = my_list[1]
#print x[0]
x = 0
for count in my_list:
    w = my_list[x]
    a = w[0]
    b = w[1]
    print (a, b)
    #print 'a',  a , 'b', b
    if a == '' and b == '' and x < 3044:
        h = x - 1
        my_list[x] = my_list[h]
        #print 'my_list[x]', my_list[x]
        x = x + 1
        #print my_list[x]
    elif a != '' and b != '' and x < 3044:
        my_list[x] = (a,b)
        x = x + 1
       # print my_list[x]
writer = csv.writer(open('C:/Users/user/Desktop/col2.csv', 'wb'))
#for count in my_list:
data = my_list
for row in data:
    writer.writerow(row)
#print count

【问题讨论】:

    标签: python-3.x export-to-csv


    【解决方案1】:

    当你说

    具有先前值的空白行

    我假设你想转:

    1234  k
    
    343   o
    
    5687  uiuuo
    

    进入

    1234  k
    1234  k
    343   o
    343   o
    5687  uiuuo
    

    你的代码有很多问题:

    import csv
    with open('col.csv', 'r') as f:
        reader = csv.reader(f)
        my_list = list(reader)
    

    如果您已将其注释掉,则无需将其包含在您的问题中

    #print my_list[1]
    #x = my_list[1]
    #print x[0]
    
    x = 0
    for count in my_list:
    

    确实知道您的列表不包含计数,对吗?这只是撒谎的代码。不要那样做。此外,如果您想枚举列表并获取索引和值,这就是 enumerate 的用途。应该是for x, value in enumerate(my_list)

        w = my_list[x]
        a = w[0]
        b = w[1]
    

    您的第二行实际上并没有两个元素。这就是您的代码失败的原因。哎呀。

        print (a, b)
        #print 'a',  a , 'b', b
    

    这里的代码是一团糟。你为什么限制在x &lt; 3044h 是一些没有意义的随机变量名。也不要那样做。

        if a == '' and b == '' and x < 3044:
            h = x - 1
            my_list[x] = my_list[h]
            #print 'my_list[x]', my_list[x]
            x = x + 1
            #print my_list[x]
        elif a != '' and b != '' and x < 3044:
            my_list[x] = (a,b)
            x = x + 1
           # print my_list[x]
    

    不要像这样打开文件,它们可能永远不会被刷新到磁盘。或者整个文件在任何情况下都不会。 始终使用with 块!

    writer = csv.writer(open('C:/Users/user/Desktop/col2.csv', 'wb'))
    #for count in my_list:
    data = my_list
    for row in data:
        writer.writerow(row)
    #print count
    

    所以...这里有一个有趣的假设 - 您的第一行 必须 不能为空。我的意思是,我想它可以,但是你会写空行,也许你不想要那样。此外,您提供的输入似乎与您所做的不匹配,因为您没有使用 \t 分隔符。

    如果您考虑自己想要做什么,您可以很容易地想出这些步骤:

    • 对于输入文件中的每一行
    • 将该行写入输出文件
    • 如果为空/空,写出上一行

    那么这很简单。

    import csv
    
    with open('input.csv') as infile, open('output.csv', 'w') as outfile:
        reader = csv.reader(infile, delimiter='\t')
        writer = csv.writer(outfile)
    
        for row in reader:
            writer.writerow(row)
    

    这可行 - 但如果我们有一个空白行,它就不会写入前一行。嗯。那么我们该怎么做呢?那么,为什么不存储前一行呢?如果当前行是空的,我们可以写上一行。

        previous_row = []  # If the first row is empty we need an empty list
                           # or whatever you want.
        for row in reader:
            if not row:
                writer.writerow(previous_row)
            else:
                writer.writerow(row)
                previous_row = row
    

    如果您想将['', ''] 视为空行,您也只需调整代码:

    if not row and not all(row):
       ...
    

    现在,如果该行为空,或者该行包含 false-y 项,它也会跳过该项。

    【讨论】:

      【解决方案2】:

      尽量不要索引空列表的元素或将它们分配给变量。 在您的情况下,最简单的方法是简单地克隆一个完整的行。

      import csv
      with open('col.csv', 'r') as f:
      reader = csv.reader(f)
         my_list = list(reader)
      for i in range(0,len(my_list)):
         currentLine = my_list[i]
         #Make sure it's not the first line and it's empty, else continue
         if not currentLine and i > 0:
             my_list[i] =my_list[i-1]
      
      with open('C:/Users/user/Desktop/col2.csv','wb') as f:
        writer = csv.writer(f)
        for row in my_list:
            writer.writerow(row)
      

      【讨论】:

      • 但它不会从有 1234 k 的行中获取前面的两个元素。
      猜你喜欢
      • 1970-01-01
      • 2018-08-21
      • 2016-04-11
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 2015-07-16
      相关资源
      最近更新 更多