【问题标题】:unexpected EOF while parsing解析时意外的 EOF
【发布时间】:2016-03-10 10:45:17
【问题描述】:

我正在开发一个程序,该程序从各种文本文件中调用数据并以用户选择的各种方式显示它。我对 Python 不是很熟悉,我的代码中可能有很多错误。当我运行代码时,我得到一个unexepected EOF while parsing 错误。有人知道这里出了什么问题吗?

from time import sleep
import sys

for i in range(21):
    sys.stdout.write('\r')
    sys.stdout.write("[%-20s] %d%%" % ('='*i, 5*i))
    sys.stdout.flush()
    sleep(0.25)

print('\nWelcome to The Arithmatic Quiz: Teacher Eddition.')

UserName = input('Please input your user name and press enter.')

if UserName == 'MrSmith':
    print ('Please proceed.')

else:
    quit()

Password = input('Please input your password and press enter.')

if Password == 'Maths':
    print ('Welcome Mr Smith.')

else:
    quit()

import csv
import operator

classdata = 0
while classdata == 0:

    Class = input ('Which class do you want the data for?')

    Display = input ('How do you want the data t be sorted? Alphabetically      (A), on highest score for the first try (H1), on highest score for the second try (H2), on highest score for the third try (H3), or average score (Avg)?')

if Class == '1':
    Class_1 = open('class_1.txt','r')

    csv1 = csv.reader(Class_1,delimiter=',')

    if Display == 'A':
        sortA = sorted(csv1,key=operator.itemgetter(1))

        for eachline in sortA:
            print (eachline)

    if Display == 'H1':
        sortB = sorted(csv1,key=operator.itemgetter(2))

        for eachline in sortB:
            print (eachline)

    if Display == 'H12':
        sortB = sorted(csv1,key=operator.itemgetter(3))

        for eachline in sortB:
            print (eachline)

    if Display == 'H3':
        sortB = sorted(csv1,key=operator.itemgetter(4))

        for eachline in sortB:
            print (eachline)

    elif Display == 'Avg':
        sortC = sorted(csv1,key=operator.itemgetter(5))

        import csv
        from collections import Counter

        def average_column (csv_filepath):
            row_totals = Counter()
            with open('Class_1',"rb") as f:
                reader = csv.reader(f)
                column_count = 0.0
                for column in reader:
                    for row_idx, row_value in enumerate(column):
                        try:
                            n = float(row_value)
                            row_totals(row_idx) += n
                            column_count += 1.0
                            column_count -= 1.0
                            row_indexes = row_totals.keys()
                            row_indexes.sort()
                            averages = (row_totals(idx)/column_count for idx in row_indexes)
                            print(averages)

如果有帮助,我正在运行 Python 3.4.3

【问题讨论】:

  • 能否请您至少发布一段您的输入文件。此外,您发布了相当多的代码,您能否更具体一点,您是否进行了调试并且您知道它在哪里失败?您提供的信息越多,您获得好的答案的可能性就越大。谢谢。
  • 欢迎来到 Stackoverflow!为什么你认为你不使用input 函数?并尝试使您的代码尽可能小(即尝试制作最小的示例以使该问题可重现)。
  • 错误是由未封闭的Try/expect指令直接产生的。
  • 我的 .txt 文件只是名称和数字,我不知道这是否有帮助...
  • 但是代码的整体结构非常混乱。从import csv 开始的部分看起来像是从某个地方复制粘贴的。您需要注意 python 中的缩进。您粘贴的函数应该从 0 缩进级别开始。

标签: python python-3.x


【解决方案1】:

您在代码末尾缺少try/except 循环的except 部分。如果你有一个try 语句,你也必须有一个except 语句,否则它会给你你看到的错误。以下是缺少except 语句的代码部分:

try:
    n = float(row_value)
    row_totals(row_idx) += n
    column_count += 1.0
    column_count -= 1.0
    row_indexes = row_totals.keys()
    row_indexes.sort()
    averages = (row_totals(idx)/column_count for idx in row_indexes)
    print(averages)
except:   # here is the except statement you were missing
    pass 

【讨论】:

  • 谢谢@MarkyPython,已经解决了。
猜你喜欢
  • 2015-12-08
  • 1970-01-01
  • 1970-01-01
  • 2013-04-25
  • 2021-03-21
  • 1970-01-01
  • 2021-01-11
相关资源
最近更新 更多