Python 读取输入文件并汇总数据

Python 读取输入文件并汇总数据,python,python-3.x,Python,Python 3.x,我正在为我的入门级计算机课程做作业,我遇到了一个错误,我无法理解它为什么会发生 我的目标(目前)是能够从输入文件中提取信息,并以这样一种方式存储它,在这种方式中我可以得到3个值——动物id、访问时间和站点 以下是输入文件: #Comments a01:01-24-2011:s1 a03:01-24-2011:s2 a03:09-24-2011:s1 a03:10-23-2011:s1 a04:11-01-2011:s1 a04:11-02-2011:s2 a04:11-03-2011:s1 a0

我正在为我的入门级计算机课程做作业,我遇到了一个错误,我无法理解它为什么会发生

我的目标(目前)是能够从输入文件中提取信息,并以这样一种方式存储它,在这种方式中我可以得到3个值——动物id、访问时间和站点

以下是输入文件:

#Comments
a01:01-24-2011:s1
a03:01-24-2011:s2
a03:09-24-2011:s1
a03:10-23-2011:s1
a04:11-01-2011:s1
a04:11-02-2011:s2
a04:11-03-2011:s1
a04:01-01-2011:s1

a02:01-24-2011:s2
a03:02-02-2011:s2
a03:03-02-2011:s1
a02:04-19-2011:s2
a04:01-23-2011:s1
a04:02-17-2011:s1

#comments
a01:05-14-2011:s2
a02:06-11-2011:s2
a03:07-12-2011:s1
a01:08-19-2011:s1
a03:09-19-2011:s1
a03:10-19-2011:s2
a03:11-19-2011:s1
a03:12-19-2011:s2
a04:12-20-2011:s2
a04:12-21-2011:s2
a05:12-22-2011:s1
a04:12-23-2011:s2
a04:12-24-2011:s2
这是我到目前为止的代码:

import os.path

def main():
    station1={}
    station2={}
    record=()
    items=[]
    animal=[]


endofprogram =False
try:
    filename1=input("Enter name of input file >")
    infile=open(filename1,"r")
    filename2=input('Enter name of output file > ')
    while(os.path.isfile(filename2)):
        filename2=input("File Exists!Enter name again>")
        outfile=open(filename2.strip(),"w")
except IOError:
    print("File does not exist")
    endofprogram=True

if endofprogram==False:
    print ('Continuing program')
    records=reading(infile)
    print('records are > ',records)




def reading(usrinput):
    for line in usrinput:
        if (len(line) !=0) or (line[0]!='#'):
                AnimalID,Timestamp,StationID =line.split()
                record= (AnimalID, Timestamp, StationID)
                data=data.append(record)
                return data

main()
我试图做的是打开文件并导入由“:”分隔的3个数据集 我一直收到的错误如下:

Continuing programTraceback (most recent call last):
  File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 39, in <module>
  File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 25, in main
  File "C:\Program Files (x86)\Wing IDE 101 5.0\src\debug\tserver\_sandbox.py", line 34, in reading
builtins.ValueError: need more than 1 value to unpack

但是仍然没有。我假设
注释
是您文件中的一行。因此,
reading
函数尝试解析的第一行就是简单的
Comments
。这将不起作用,因为在空白处拆分行时,
注释
不会创建三个元素长的序列:

AnimalID, Timestamp, StationID = line.split()  # won't work with headings
由于输入文件最近的格式设置,如果过滤要拆分的行(也就是说,确保要拆分的行始终有两个冒号,这将为您提供三个元素),则可以使用上述方法。以下方法可能会说明一种可用于激发思维的替代方法:

for line in lines:  # from the open file
    if ':' in line.strip():  # for example; need to distinguish from station visits from headings somehow
        print(line.split(':'))  # you don't really want to print here, but you should figure out how to store this data
正如我在评论中所说,你不会真的想打印最后一行;您希望将其存储在某种数据结构中。此外,您可能会找到更好的方法来区分有车站访问的线路和没有车站访问的线路。我将把这些问题留给你去解决,因为我不想破坏你剩下的作业。

问题是
len(line)=0
始终为
。要选择不以
#
开头的非空行,您可以:

line = line.strip() # remove leading/trailing whitespace
if line and line[0] != '#': 
   fields = line.split(':') #NOTE: use ':' delimiter
   if len(fields) == 3:
      data.append(fields)

刚刚加上去的。但简言之,我试图用:分隔这些数据值,但如果行中的“:”还不够,我的代码
将一事无成。如果行是:
#a:b:c
line = line.strip() # remove leading/trailing whitespace
if line and line[0] != '#': 
   fields = line.split(':') #NOTE: use ':' delimiter
   if len(fields) == 3:
      data.append(fields)