Python 2.7 索引器错误,但更可能是I/O错误

Python 2.7 索引器错误,但更可能是I/O错误,python-2.7,Python 2.7,不确定我为什么会出现这个错误。我正在读取一个名为columns\u unsorted.txt的文件,然后尝试写入columns\u unsorted.txt。fan_on=string_j[1]上出现错误,表示列表索引超出范围。这是我的密码: #!/usr/bin/python import fileinput import collections # open document to record results into j = open('./columns_unsorted.txt'

不确定我为什么会出现这个错误。我正在读取一个名为columns\u unsorted.txt的文件,然后尝试写入columns\u unsorted.txt。fan_on=string_j[1]上出现错误,表示列表索引超出范围。这是我的密码:

#!/usr/bin/python

import fileinput
import collections

# open document to record results into
j = open('./columns_unsorted.txt', 'r')
# note this is a file of rows of space-delimited date in the format <1384055277275353 0 0 0 1 0 0 0 0 22:47:57> on each row, the first term being unix times, the last human time, the middle binary indicating which machine event happened

# open document to read from
l = open('./columns_sorted.txt', 'w')

# CREATE ARRAY CALLED EVENTS
events = collections.deque()
i = 1

# FILL ARRAY WITH "FACTS" ROWS; SPLIT INTO FIELDS, CHANGE TYPES AS APPROPRIATE
for line in j:      # columns_unsorted
    line = line.rstrip('\n')
    string_j = line.split(' ')
    time = str(string_j[0])
    fan_on = int(string_j[1])
    fan_off = int(string_j[2])    
    heater_on = int(string_j[3])
    heater_off = int(string_j[4])
    space_on = int(string_j[5])
    space_off = int(string_j[6])
    pump_on = int(string_j[7])
    pump_off = int(string_j[8])
    event_time = str(string_j[9])

    row = time, fan_on, fan_off, heater_on, heater_off, space_on, space_off, pump_on, pump_off, event_time
    events.append(row)
#/usr/bin/python
导入文件输入
导入集合
#打开文档以将结果记录到
j=打开('./列_unsorted.txt',r')
#注意:这是一个以空格分隔的日期行的文件,格式为每行,第一个术语是unix时间,最后一个人时间,中间的二进制表示发生了哪台机器事件
#打开要从中读取的文档
l=打开('./列_sorted.txt',w')
#创建名为事件的数组
events=collections.deque()
i=1
#用“事实”行填充数组;拆分为字段,根据需要更改类型
对于j中的行:#列_未排序
line=line.rstrip('\n')
string_j=line.split(“”)
time=str(字符串_j[0])
fan_on=int(字符串_j[1])
fan_off=int(字符串_j[2])
加热器开=整数(字符串[3])
加热器关闭=int(字符串[4])
空格_on=int(字符串_j[5])
空格_off=int(字符串_j[6])
泵开=int(字符串[7])
泵关闭=int(字符串[8])
事件时间=str(字符串[9])
行=时间、风扇打开、风扇关闭、加热器打开、加热器关闭、空间打开、空间关闭、泵打开、泵关闭、事件时间
events.append(行)

您缺少
读线功能,不是吗

你必须做到:

 j = open('./columns_unsorted.txt', 'r')
 l = j.readlines()
 for line in l:
     # what you want to do with each line

将来,您应该打印一些变量,以确保代码按您希望的方式工作,并帮助您识别问题。
(例如,如果在您的代码中,您将
打印字符串_j
,您将看到您遇到了什么类型的问题)

问题是数据文件中的一行不一致。请原谅我匆忙发帖