0

Have an if statement that goes and downloads a file from a server. When it's not in the if statement the script runs fine, however, within the if statement it gives me an error. Any idea?

    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',
            } 

Traceback (most recent call last): File "pandas/_libs/parsers.pyx", line 1169, 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

> 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
0

1 Answer 1

1

'utf-8' codec can't decode byte 0xa0 in position 1: invalid start byte usually means it's a character (e.g., smart quotes) that can't be decoded into a unicode string. Thus, the problem is most likely in the .csv document, not in the code itself.

To handle it, you can pass an explicit encoding argument to read_csv(), like so:

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

Here is a nice list of Python 3's standard encodings.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.