IndexError: list index out of range reading/writing from/to file

前端 未结 3 485
悲&欢浪女
悲&欢浪女 2021-01-27 09:13

I have got this script on Python 2.6 with RHEL OS:

import csv

def write_cols(data):
    col_spacer = \"   \"      # added between columns
    widths = [0] * len         


        
相关标签:
3条回答
  • 2021-01-27 09:28

    I solved with the help of @MartinEvans.

    The problem is simplier than we could imagine: there was a blank line on the bottom of the input file.

    0 讨论(0)
  • 2021-01-27 09:30

    Your format call here:

    "{{:<{width}}}".format(col, width=widths[index])
    

    has either too many or not enough arguments / braces. The braces in the string each indicate a place to put text for format. I'd say you have too many braces and it gets confused by that. You could either use different parenthesis, or remove them...

    Tell me if i'm missing the point/purpose of this statement

    0 讨论(0)
  • 2021-01-27 09:35

    You need to change this

    "{:<{width}}".format(col, width=widths[index])
    

    to this

    "{0:<{1}}".format(col, widths[index])
    

    and it will work.

    0 讨论(0)
提交回复
热议问题