带有 read_csv 的嵌套 If 语句 [重复]

Posted

技术标签:

【中文标题】带有 read_csv 的嵌套 If 语句 [重复]【英文标题】:Nested If statement with read_csv [duplicate] 【发布时间】:2019-10-08 07:00:49 【问题描述】:

有一个 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',
             

Traceback(最近一次调用最后一次): 文件“pandas/_libs/parsers.pyx”,第 1169 行,在 pandas._libs.parsers.TextReader._convert_tokens 文件“pandas/_libs/parsers.pyx”,第 1299 行,在 pandas._libs.parsers.TextReader._convert_with_dtype 文件“pandas/_libs/parsers.pyx”,第 1315 行,在 pandas._libs.parsers.TextReader._string_convert 文件“pandas/_libs/parsers.pyx”,第 1553 行,在 pandas._libs.parsers._string_box_utf8 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】:

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

要处理它,您可以将显式 encoding 参数传递给 read_csv(),如下所示:

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

Here 是 Python 3 标准编码的一个很好的列表。

【讨论】:

以上是关于带有 read_csv 的嵌套 If 语句 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

带有嵌套 if/else 语句的 While 循环

带有字典、列表和 If 语句的嵌套循环

带有 if 语句意外结果的 Python 嵌套循环

带有嵌套 if 语句的 while 循环的 Big-O 运行时

嵌套的 if 语句、简洁的代码和 Pythonic,带有控制器/键盘输入

带有 JSON 的 Razor if 语句 [重复]