Python Forum
problem coverting string data file to dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem coverting string data file to dictionary
#21
in your code you have 2 loops, but they work separately. In addition at the end of first loop item is actually the last pair of key and value

when you try to pass it to dict() function, it tries to make key:value pair from first element, which is in fact string with len 5 e.g. p0004
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#22
you unnecessary complicate it. No need for [] around item.split(':') - you make a list wit one element - again list, but with 2 elements. As a result you also add a loop. Look at my latest code
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#23
You are right. line = [item.split(':')] creates the need for a separate loop. converting it to item = item.split(':') dispenses with the need for a second loop.

Thanks for explaining and your patience.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matching Data - Help - Dictionary manuel174102 1 356 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Convert string to float problem vasik006 8 3,269 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Converting '1a2b3c' string to Dictionary PythonNoobLvl1 6 1,781 May-13-2022, 03:44 PM
Last Post: deanhystad
  [SOLVED] Concat data from dictionary? Winfried 4 1,666 Mar-30-2022, 02:55 PM
Last Post: Winfried
Question How do I skipkeys on json file read to python dictionary? BrandonKastning 3 1,831 Mar-08-2022, 09:34 PM
Last Post: BrandonKastning
  trying to write a dictionary in a csv file CompleteNewb 13 6,385 Mar-04-2022, 04:43 AM
Last Post: deanhystad
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,524 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  f string concatenation problem growSeb 3 2,213 Jun-28-2021, 05:00 AM
Last Post: buran
Question Problem with string and \n Falassion 6 2,618 Jun-15-2021, 03:59 PM
Last Post: Falassion

Forum Jump:

User Panel Messages

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