2

我有一个 python 程序,它基本上解析一些 CSV 并打印出一行,然后停止,直到用户点击输入。这是完整的代码:

#!/usr/bin/python

import os
import csv
import sys
from datetime import datetime

teams = [[] for x in xrange(0, 400)]
counter = 0

with open('t26.csv', 'rb') as f:

    next(f)

    reader = csv.reader(f)

    for row in reader:
        if row:
            if row[1] <> "" and row[1] <> "TEAM AVERAGES:":
                teams[counter].append(row[16])
                teams[counter].append(row[3])
                teams[counter].append(row[0])
                teams[counter].append(row[4])
                counter += 1


for i in range(0, counter - 1):
    diff = False
    lastTeam = ""
    firstDate = ""

    eid = teams[i][0]
    date = teams[i][1]
    team = teams[i][2]
    pc = teams[i][3] 


    for csvfile in os.listdir('Uploads'):
        with open('Uploads/' + csvfile, 'rb') as f:
            reader = csv.reader(f)

            team_index = 0
            eid_am_index = 0
            eid_pm_index = 0
            find = False

            for row in reader:

                index = 0
                for column_name in row:
                    if "team" == column_name:
                        team_index = index
                    if "eid_am" in column_name:
                        eid_am_index = index
                    if "eid_pm" in column_name:
                        eid_pm_index = index
                    index += 1


                if eid in row:
                    #print row[team_index] + ', ' + row[eid_am_index] + ', ' + row[eid_pm_index] + ', ' + ' ----> ' + csvfile
                    if row[team_index] <> lastTeam and lastTeam <> "":
                        diff = True
                    lastTeam = row[team_index]
                    if firstDate == "":
                        firstDate = csvfile
                    break


    if diff:
        print "\n*diff"
    else: #teams are the same
        team = team[5:]

        if "(" in team:
            team = team[:team.index('(') - 1]

        try:
            lastTeam = lastTeam[:lastTeam.index(' ')]
        except:
            g = 0
        print "\n*no diff: " + eid + " --> " + firstDate + " | " + date + "\tTeam: " + team + " | " + lastTeam + "\tPC: " + pc
        if team <> lastTeam and lastTeam <> "":
            print "*(!) teams not equal"

        f = raw_input('') #read user input and do nothing with it

我在 Windows 上的 Ubuntu 上的 Bash 上运行这个程序,有时终端上会随机弹出符号“^@”,然后当我单击 Enter 时出现错误。

这是终端外观的示例(带有一些#comments 来解释):

*no diff: 4903 --> 6-27-2017 3_44_01 PM.csv | 8/1/2017 1:56:39 PM       Team: 180-A | 180-A     PC: AGENT3-102 #this line is printed out by the python program
^@ #this randomly show up
Traceback (most recent call last): #when i hit enter i get this error
  File "parse.py", line 127, in <module>
    f = raw_input('')
EOFError

这里还有一个截图: 在此处输入图像描述

4

0 回答 0