2016-11-14 20 views
0

有誰知道是否有可能避免錯誤ValueError: zero length field讀/ /寫入到文件與Python 2.6.6按規定在以下幾點:ValueError異常:在讀取和格式零長度字段的名字寫

File "format.py", line 26, in <module> 
    for row in write_cols(rows): 
    File "format.py", line 19, in write_cols 
    return [col_spacer.join("{:<{width}}".format(col, width=widths[index]) for index, col in enumerate(row)) for row in data] 
    File "format.py", line 19, in <genexpr> 
    return [col_spacer.join("{:<{width}}".format(col, width=widths[index]) for index, col in enumerate(row)) for row in data] 
ValueError: zero length field name in format 

回答

1

變化

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

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

堂妹在蟒蛇的老版本,你需要指出多格式字段的順序。

+0

編輯我的代碼,但它不起作用。更多細節在這裏http://stackoverflow.com/questions/40589550/indexerror-list-index-out-of-range-reading-writing-writing-from-to-file –

+0

確保你編寫'「{0:<{1} }「.format(col,widths [index])''而不是'」{0:<{1}}「格式(col,width = widths [index])' –

+0

完成。我有這個錯誤:對於write_cols(行)中的行: 文件「format.py」,第11行,在write_cols中 return [col_spacer.join(「{0:<{1}}」。format(col,widths [ index]]爲索引,col in enumerate(row))爲行中的數據] 文件「format.py」,第11行,在 return [col_spacer.join(「{0:<{1}}」.format (col,width [index])for index,col in enumerate(row))for row in data] IndexError:列表索引超出範圍 –

相關問題