带read_csv的嵌套If语句

时间:2019-05-21 20:19:34

标签: python python-3.x pandas dataframe

具有一条if语句,该语句可以从服务器下载文件。如果不在if语句中,脚本可以正常运行,但是,在if语句中,脚本给我一个错误。有想法吗?

    leagues = {
            '1': 'Premier League',
            '2': 'Championship League',
            '3': 'League 1',
            '4': 'League 2',
            '5': 'Conference League'
        }

        # Choose which league to model
        print("Please pick which league:\n")
        for ref, league in leagues.items():
            print(ref, ":", league)

        leagueChoice = input("Enter League Number: ")

        if leagueChoice == 1:
            df = pd.read_csv(
                "http://www.football- data.co.uk/mmz4281/{}/E0.csv".format(season_year))

            england_teams = {
                '1': 'Arsenal',
            }

            print(df.head)
            exit()
        elif leagueChoice == 2:
            df = pd.read_csv(
                "http://www.football-data.co.uk/mmz4281/{}/E1.csv".format(season_year))

            england_teams = {
                '1': 'Aston Villa',
            }
        elif leagueChoice == 3:
            df = pd.read_csv(
                "http://www.football-data.co.uk/mmz4281/{}/E2.csv".format(season_year))

            england_teams = {
                '1': 'Accrington',
            }
        elif leagueChoice == 4:
            df = pd.read_csv(
                "http://www.football-data.co.uk/mmz4281/{}/E3.csv".format(season_year))

            england_teams = {
                '1': 'Bury',
            }
        else:
            df = pd.read_csv(
                "http://www.football-data.co.uk/mmz4281/{}/EC.csv".format(season_year))

            england_teams = {
                '1': 'Aldershot',
            } 
  

回溯(最近通话最近):     在pandas._libs.parsers.TextReader._convert_tokens中的文件“ pandas / _libs / parsers.pyx”,行1169     在pandas._libs.parsers.TextReader._convert_with_dtype中的文件“ pandas / _libs / parsers.pyx”,行1299     在pandas._libs.parsers.TextReader._string_convert中的文件“ pandas / _libs / parsers.pyx”,第1315行     在pandas._libs.parsers._string_box_utf8中,文件“ pandas / _libs / parsers.pyx”,行1553   UnicodeDecodeError:“ utf-8”编解码器无法解码位置1的字节0xa0:无效的起始字节

> During handling of the above exception, another exception occurred:

> Traceback (most recent call last):
  File "Soccer.py", line 794, in <module>
    main_menu()
  File "Soccer.py", line 64, in main_menu
    exec_menu(choice)
  File "Soccer.py", line 77, in exec_menu
    menu_actions[ch]()
  File "Soccer.py", line 571, in england
    "http://www.football-data.co.uk/mmz4281/{}/EC.csv".format(season_year))
  File "/Users/user/anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py", line 702, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/Users/user/anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py", line 435, in _read
    data = parser.read(nrows)
  File "/Users/user/anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py", line 1139, in read
    ret = self._engine.read(nrows)
  File "/Users/user/anaconda3/lib/python3.6/site-packages/pandas/io/parsers.py", line 1995, in read
    data = self._reader.read(nrows)
  File "pandas/_libs/parsers.pyx", line 899, in pandas._libs.parsers.TextReader.read
  File "pandas/_libs/parsers.pyx", line 914, in pandas._libs.parsers.TextReader._read_low_memory
  File "pandas/_libs/parsers.pyx", line 991, in pandas._libs.parsers.TextReader._read_rows
  File "pandas/_libs/parsers.pyx", line 1123, in pandas._libs.parsers.TextReader._convert_column_data
  File "pandas/_libs/parsers.pyx", line 1176, in pandas._libs.parsers.TextReader._convert_tokens
  File "pandas/_libs/parsers.pyx", line 1299, in pandas._libs.parsers.TextReader._convert_with_dtype
  File "pandas/_libs/parsers.pyx", line 1315, in pandas._libs.parsers.TextReader._string_convert
  File "pandas/_libs/parsers.pyx", line 1553, in pandas._libs.parsers._string_box_utf8
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 1: invalid start byte

1 个答案:

答案 0 :(得分:1)

'utf-8' codec can't decode byte 0xa0 in position 1: invalid start byte通常表示它是一个字符(例如,智能引号),无法解码为unicode字符串。因此,问题很可能出现在.csv文档中,而不是在代码本身中。

要处理此问题,可以将一个明确的encoding参数传递给read_csv(),如下所示:

df = pd.read_csv("<file>".format(season_year), encoding='<type>')

Here是Python 3的标准编码的不错列表。