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
#8
@buran & Larz60+:

Thank you both for your input. One thing that is definitely clear, is to avoid use of eval(). I had read about the dangers but did not know of an alternative. Not familiar with 'json' files. will definitely look into it.
Using the suggestion, I was able to output tstdic.txt (and most of the orginal data file) as:
Output:
{"voornaam": "Thomas", "geslacht": "M", "t8306": "26794", "n8589": "4856", "n9094": "6559", "n9599": "6412", "n0004": "5897", "p8589": "8972", "p9094": "11424", "p9599": "11760", "p0004": "11324"}
format as suggested by buran, using:
line = line.strip().replace("'", '"').replace('"{', "{").replace('}"', "}")
and as expected,
d1 = json.loads(line)
then created dictionary without using eval().

The problem with converting "voornaam" values containing an apostrophe, however, still remains. I can't seem to find a way to reformat these entries to conform to json or eval. For that reason may be I will keep the topic open.

Thanks again.
Reply


Messages In This Thread
RE: problem coverting string data file to dictionary - by AKNL - Mar-07-2020, 11:16 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace values in Yaml file with value in dictionary PelleH 1 2,093 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  Matching Data - Help - Dictionary manuel174102 1 1,083 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  Need to replace a string with a file (HTML file) tester_V 1 1,869 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Convert string to float problem vasik006 8 5,282 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Converting '1a2b3c' string to Dictionary PythonNoobLvl1 6 3,249 May-13-2022, 03:44 PM
Last Post: deanhystad
  [SOLVED] Concat data from dictionary? Winfried 4 2,731 Mar-30-2022, 02:55 PM
Last Post: Winfried
Question How do I skipkeys on json file read to python dictionary? BrandonKastning 3 2,730 Mar-08-2022, 09:34 PM
Last Post: BrandonKastning
  trying to write a dictionary in a csv file CompleteNewb 13 10,516 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,194 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  f string concatenation problem growSeb 3 3,044 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