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
  Replace values in Yaml file with value in dictionary PelleH 1 2,179 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  Matching Data - Help - Dictionary manuel174102 1 1,127 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  Need to replace a string with a file (HTML file) tester_V 1 1,949 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Convert string to float problem vasik006 8 5,457 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Converting '1a2b3c' string to Dictionary PythonNoobLvl1 6 3,340 May-13-2022, 03:44 PM
Last Post: deanhystad
  [SOLVED] Concat data from dictionary? Winfried 4 2,794 Mar-30-2022, 02:55 PM
Last Post: Winfried
Question How do I skipkeys on json file read to python dictionary? BrandonKastning 3 2,757 Mar-08-2022, 09:34 PM
Last Post: BrandonKastning
  trying to write a dictionary in a csv file CompleteNewb 13 10,819 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 4,292 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  f string concatenation problem growSeb 3 3,093 Jun-28-2021, 05:00 AM
Last Post: buran

Forum Jump:

User Panel Messages

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