Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 invalid literal for int() with base 10: ''
#1
Getting an error "invalid literal for int() with base 10: ''" on the line where I assign the keys.

What is the best way to debug?
# dictionary keys will be the (Club and year)
premier = {} 
print()
# open the file
with open(r"League_Table.csv") as data_file:
    # read in the first line containing the headers
    headers = data_file.readline()
    
    # for each other line in the file
    for line in data_file:
        # split each line into components (remove white space from ends of line)
        Team,Pos,Pld,HW,HD,HL,HG,HC,AW,AD,AL,AG,AC,W,D,L,G,C,DF,Pts,Champ,Rel,Year = line.strip().split(",")
        # insert the data into the dictionary
        premier[str(Team),int(Year)] = ((int(Pos), int(Pld), int(HW), int(HD), int(HL), int(HG), int(HC), int(AW), int(AD), int(AL), int(AG), int(AC), int(W), int(D), int(L), int(G), int(C), int(DF), int(Pts), Champ, Rel))
Output:
Team,Pos,Pld,HW,HD,HL,HG,HC,AW,AD,AL,AG,AC,W,D,L,G,C,DF,Pts,Champ,Rel,Year Manchester United,1,42,14,5,2,39,14,10,7,4,28,17,24,12,6,67,31,36,84,Yes,No,1993 Aston Villa,2,42,13,5,3,36,16,8,6,7,21,24,21,11,10,57,40,17,74,No,No,1993 Norwich City,3,42,13,6,2,31,19,8,3,10,30,46,21,9,12,61,65,-4,72,No,No,1993
ichabod801 wrote Dec-03-2019, 04:04 PM:
Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time.
Quote
#2
The way you are reading the data into individual variables makes it harder to debug. It would be easier if you had that info in a list or dictionary. Especially since it doesn't seem like you are doing much with those variables.

I would start by printing team and year right before line 14. That way you will know which line in the data is causing the problem (the last one printed), and you can check that like to make sure it has valid data.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
(Dec-03-2019, 04:11 PM)ichabod801 Wrote: The way you are reading the data into individual variables makes it harder to debug. It would be easier if you had that info in a list or dictionary. Especially since it doesn't seem like you are doing much with those variables.

I would start by printing team and year right before line 14. That way you will know which line in the data is causing the problem (the last one printed), and you can check that like to make sure it has valid data.

Thank you for your reply.

The column DF has negative values, could that be the issue?
Quote
#4
(Dec-03-2019, 04:39 PM)mrsenorchuck Wrote: The column DF has negative values, could that be the issue?

No. The error indicates you are getting an empty string somewhere in your data. You might search your data for ',,' (two consecutive commas). However, there could be other ways to get that error. In any case, the print method I mentioned will show you what line the problem is on.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#5
(Dec-03-2019, 04:53 PM)ichabod801 Wrote:
(Dec-03-2019, 04:39 PM)mrsenorchuck Wrote: The column DF has negative values, could that be the issue?

No. The error indicates you are getting an empty string somewhere in your data. You might search your data for ',,' (two consecutive commas). However, there could be other ways to get that error. In any case, the print method I mentioned will show you what line the problem is on.
Thank you
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  ValueError: invalid literal for int() with base 10: '' Jay123 7 1,293 Aug-05-2019, 02:43 PM
Last Post: Jay123
  ValueError: invalid literal for int() with base 10: '' ivinjjunior 6 4,162 Apr-20-2019, 05:37 PM
Last Post: keames
  Problem with "invalid literal for int() with base 10: '' jirkaj4 4 5,199 Jan-23-2018, 06:55 PM
Last Post: jirkaj4
  ValueError: invalid literal for int() with base 10: '[2,4,7,8,19]' rajeev1729 3 46,475 Sep-15-2017, 05:51 PM
Last Post: nilamo
  why does integer input give error invalid literal for int() PyPhanman 2 2,931 Apr-27-2017, 05:32 AM
Last Post: ndc85430
  ValueError: invalid literal for int() with base 10: '' DennisT 8 8,484 Oct-12-2016, 03:20 PM
Last Post: DennisT

Forum Jump:


Users browsing this thread: 1 Guest(s)