[英]List index out of range in 'file' object — Python
I have a text file reval2_log.txt
, which contains data like 我有一个文本文件
reval2_log.txt
,其中包含数据
yield_curve on;DATE_val 2013 12 31;arion on;INFL_filn /srv/www/tbg/file/1/2014-07-21/Vaxtaferlar_nidur.csv;ASSETS_filn /srv/www/tbg/file/1/2014-07-21/Vordur_VT.xls;REVAL_filn kfjh.xls;CPI_value 416.7;LSK_value 8022.3;YIELDC_filn /srv/www/tbg/file/1/2014-07-21/infl.csv;CASHF_filn weouhf.xls;sjf on;user 1
I then do this: 然后我这样做:
f = open('reval2_log.txt', 'r')
read = f.readlines()[0].split(";")
And it returns an IndexError
. 它返回一个
IndexError
。 Python of course can find the file, because it doesn't return a "file not found" error (and it does if I garble the filename). Python当然可以找到该文件,因为它不会返回“找不到文件”错误(如果我把文件名弄乱的话)。
Trace: 跟踪:
File "/srv/www/tbg/notendur/views.py" in reikna_reval2
79. reval2.main(user)
File "/srv/www/tbg/calc/reval2.py" in main
322. read = f.readlines()[0].split(";")
Exception Type: IndexError at /notendur/reikna/
Exception Value: list index out of range
Martijn's suggestion to use readline() is good. Martijn建议使用readline()是好的。 If your input can be empty at times, then you can try this:
如果您的输入有时可能为空,那么您可以尝试这样做:
read = f.read().split(";")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.