Python Forum
List index out of range when turning CSV into dict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
List index out of range when turning CSV into dict
#11
(May-10-2021, 07:25 PM)perfringo Wrote:
(May-10-2021, 06:36 PM)ranbarr Wrote: It's an Excel but I saved it as CSV

If it is saved as CSV UTF-8 (Comma delimited) (.csv) it should have commas (or other separator) between values. Sample data you provided doesn't have commas. Empty values should be also be present between commas.

Its saved as CSV (comma delimited), not UTF - 8
Reply
#12
@ranbarr: About your code: although there is room for improvement your code should work. The problem is in the data.

About saving your data as CSV: I don't have Excel so I cannot check, but when saving your data as CSV, is there an option to choose a separator? It seems a space is chosen as separator.
If there is no option to choose a separator, please read How to change the delimiter when exporting CSV from Excel.
Reply
#13
(May-11-2021, 08:24 AM)ibreeden Wrote: Excel so I canno
I tried to save the file as CSV UTF and just as CSV after changing the delimeter and list seperation as the guide said.. still getting the same output
Output:
{'1': {'Gold': '1', 'Silver': '1', 'Bronze': '1', 'Total': '1'},
Reply
#14
(May-11-2021, 07:52 AM)ranbarr Wrote:
(May-10-2021, 07:25 PM)perfringo Wrote: CSV UTF-8 (Comma delimited) (.csv)

Its saved as CSV (comma delimited), not UTF - 8

My up-to-date Excel under macOS has this exact option to save as csv. There are other options as well (Comma Separated Values (.csv), Macintosh Comma Separated (.csv), MS-DOS Comma Separated (.csv)) but this one should be preferred as 'universal encoding'. However, regardless of saved encoding separator must be present anyway.

For those of us who use Mac and have ; as separator:

Quote:For Mac OS the chosen value separator depends on Region and region's default number separators. To change them, go to “System Preferences” > “Language & Region”. Then you need to verify the number separators in “Advanced …” > “General”.

Here are the rules Excel uses to determine how it saves CSV files:

1. If your language / region / locale uses comma (,) for decimal separation then Excel will save using semi-colons (;)
2. If it uses a dot (.) full stop or period, then it will delimit with commas (,).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#15
(May-11-2021, 09:23 AM)ranbarr Wrote: I tried to save the file as CSV UTF and just as CSV after changing the delimeter and list seperation as the guide said.. still getting the same output
Output:
{'1': {'Gold': '1', 'Silver': '1', 'Bronze': '1', 'Total': '1'},

Not so hasty, programming is a matter of stepwise refinement Smile . First look at the CSV file. Use NOTEPAD.EXE or vi to inspect it. Does it contain separators as it should?
Second: ajust your code to match the field separator and show us your code. And then also show the output. The complete output, not the line ending with a comma as you did. And if there are errors we want to see the complete error message.
Reply
#16
(May-11-2021, 12:57 PM)ibreeden Wrote:
(May-11-2021, 09:23 AM)ranbarr Wrote: I tried to save the file as CSV UTF and just as CSV after changing the delimeter and list seperation as the guide said.. still getting the same output
Output:
{'1': {'Gold': '1', 'Silver': '1', 'Bronze': '1', 'Total': '1'},

Not so hasty, programming is a matter of stepwise refinement Smile . First look at the CSV file. Use NOTEPAD.EXE or vi to inspect it. Does it contain separators as it should?
Second: ajust your code to match the field separator and show us your code. And then also show the output. The complete output, not the line ending with a comma as you did. And if there are errors we want to see the complete error message.

Yeah hasty is indeed my problem lol... I did all this and changed the code and it worked! TY!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix list index out of range longmen 26 5,927 Apr-27-2022, 05:46 PM
Last Post: deanhystad
  list index out of range OliverG 3 2,341 Sep-03-2021, 12:04 AM
Last Post: itsmycode
  Index List a04j 2 2,927 Jul-10-2021, 01:14 PM
Last Post: BashBedlam
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,899 May-14-2021, 07:14 AM
Last Post: perfringo
  List vs index: Frederico_Caldas 5 3,601 Jul-03-2020, 10:55 AM
Last Post: DeaD_EyE
  To find the index of the first occurrence of the key in the provided list Angry_bird89 4 3,258 Jun-20-2020, 06:53 PM
Last Post: Angry_bird89
  Python Adding +1 to a list item cointained in a dict ElReyZero 1 2,076 Apr-30-2020, 05:12 AM
Last Post: deanhystad
  Dict from list - HELP! PLEASE! cherry_cherry 16 5,530 Apr-09-2020, 04:01 AM
Last Post: cherry_cherry
  list index out of range mcgrim 2 2,910 May-25-2019, 07:44 PM
Last Post: mcgrim
  Turning column of 8 digit numbers into three seperate columns Wade334 1 2,180 May-11-2019, 02:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020