简体   繁体   English

拆分文本时出现“ValueError:int() 以 10 为基数的无效文字:''”

[英]I get “ValueError: invalid literal for int() with base 10: ''” when splitting text

This is my assignment:这是我的任务:

1

I am currently on part 2a (Print all the players that played more than 1700 minutes).我目前在第 2a 部分(打印所有上场时间超过 1700 分钟的球员)。

This is my code so far:到目前为止,这是我的代码:

def part1():

    createfile=open("Assignment4.txt", "a+")
    createfile.write(f"Player Name          MinsPlayed  Goals   Assists YellowCard\n")
    createfile.write(f"Lionel Messi     1943        19      4       4\n")
    createfile.write(f"Robert Lewandowski   1864        28      6       2\n")
    createfile.write(f"Harry Kane           2017        14      11      1\n")
    createfile.write(f"Jack Grealish        1977        6       10      6\n")
    createfile.write(f"Cristiano Ronaldo    1722        19      3       1\n")
    createfile.write(f"Zlatan Ibrahimovic   1102        14      1       2\n")
    createfile.write(f"Gerard Moreno        1735        14      2       3\n")
    createfile.write(f"Romelu Lukaku        1774        18      6       4\n")
    createfile.write(f"Kylian Mbappe        1706        18      6       3\n")
    createfile.write(f"Erlin Haaland        1542        17      4       2")
    createfile.close()

part1()

def part2():

    filetoopen=open("Assignment4.txt", "r")
    for line in filetoopen.readlines():    
        linetosplit=line.split(' ')
        players = []
        player_name = (linetosplit[0] + ' ' + linetosplit[1])
        mins_played = (linetosplit[2])
        goals = (linetosplit[3])
        assists = (linetosplit[4])
        yellow_card = (linetosplit[5])
        players.append([player_name, mins_played, goals, assists, yellow_card]) 
    filetoopen.close()
            
    if mins_played > 1700:
        print(player_name)

part2()

When I run it this error message pops up当我运行它时,会弹出此错误消息

TypeError: '>' not supported between instances of 'str' and 'int'类型错误:“str”和“int”的实例之间不支持“>”

I then tried fixing it by changing然后我尝试通过更改来修复它

mins_played = (linetosplit[2])

to

mins_played = int(linetosplit[2])

but then this error message popped up但随后弹出此错误消息

ValueError: invalid literal for int() with base 10: '' ValueError: int() 以 10 为底的无效文字:''

Check what linetosplit actually returns.检查linetosplit实际返回的内容。 In this case you will see that it returns在这种情况下,您会看到它返回

['Player', 'Name', '', '', '', '', '', '', '', '', '', 'MinsPlayed', '', 'Goals', '', '', 'Assists', 'YellowCard\n']

['Lionel', 'Messi', '', '', '', '', '1943', '', '', '', '', '', '', '', '19', '', '', '', '', '', '4', '', '', '', '', '', '', '4\n']

['Robert', 'Lewandowski', '', '', '1864', '', '', '', '', '', '', '', '28', '', '', '', '', '', '6', '', '', '', '', '', '', '2\n']
...

As all the spaces have been split.因为所有的空间都被分割了。 As mentioned by @Reti43 there is a difference between line.split(' ') and line.split() .正如@Reti43 所述, line.split(' ')line.split()之间存在差异。

However, in your case that does not entirely solve your issue.但是,在您的情况下,这并不能完全解决您的问题。 As the first line of the file is the definition of what each column is.由于文件的第一行是每列的定义。 And you cannot convert this string to an integer.而且您不能将此字符串转换为 integer。

One way around this is to not include the first line when looping.解决此问题的一种方法是在循环时不包括第一行。 There are multiple different ways of doing this, but I did it using list manipulation by excluding the first item of the list.有多种不同的方法可以做到这一点,但我通过排除列表的第一项来使用列表操作。

for line in filetoopen.readlines()[1:]:
    # Code

This excludes the first line.这不包括第一行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到此错误: ValueError: invalid literal for int() with base 10: '\n' - I get this error: ValueError: invalid literal for int() with base 10: '\n' 我通常会收到此错误: ValueError: invalid literal for int() with base 10 - i usually get this error : ValueError: invalid literal for int() with base 10 Kaprekar数字:我得到ValueError:以10为底的int()的无效文字'' - Kaprekar numbers: I Get ValueError: invalid literal for int() with base 10 '' 当我运行以下代码时,出现以下错误:ValueError:int()以10为底的无效文字:“((1,0,'Friday')” - When I run the following code,I get this error:ValueError: invalid literal for int() with base 10: “(1, 0, 'Friday')” 将字符串从 txt 文件转换为 integer 时,出现 ValueError: invalid literal for int() with base 10: - When converting string to integer from txt file, I get ValueError: invalid literal for int() with base 10: python requests.get ValueError: int() 以 10 为基数的无效文字:'' - python requests.get ValueError: invalid literal for int() with base 10: '' Discord.py音乐bot,使用文本文件保存音量整数,但我得到ValueError:int()的无效文字,基数为10:” - Discord.py music bot, using a text file to save volume integer but I get ValueError: invalid literal for int() with base 10: '' ValueError:int() 的无效文字,基数为 10: ' ' 之前工作时 - ValueError: invalid literal for int() with base 10: ' ' when it worked before ValueError: int() 的无效文字,以 10 为基数:运行 unittest 时为“30.0” - ValueError: invalid literal for int() with base 10: '30.0' when running unittest ValueError: int() 以 10 为基数的无效文字:'' 询问输入时 - ValueError: invalid literal for int() with base 10: '' when asking for input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM