Python Forum
Issue in reading a text file contains dict data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue in reading a text file contains dict data
#1
Hi,
I am new to python. I have a text file contains data in below format.
{"ts": "2018-09-10 10:20:11.312151","action":0,"type":0,"Query":"SELECT COL1,COL2\n FROM TABLE1\LIMIT 1","ID":2}
{"ts": "2018-09-10 10:20:11.312151","action":"","type":0,"Query":"","ID":2} like that.
Expected Output:
key1 key2 key3 key4 ...
val1 val2 val3 ...
val1 val2 val3 ...

How do I read the file and parse the data?

Thanks.
Reply
#2
Is every dict on a different line? No commas between? is the example correct, e.g. TABLE1\LIMIT I think should be TABLE1\nLIMIT?
I would go to read each line and parse it as json string.then use csv module to write to csv file.
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
#3
Yes. Every dictionary is in different line. And TABLE\n LIMIT is the correct one. what is the code should I use?

Thanks.
Reply
#4
(Sep-27-2018, 06:45 AM)buran Wrote: read each line and parse it as json string.then use csv module to write to csv file.
try to write a code and post it here if you have problem
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
#5
Is this verbatim:
Quote:{"ts": "2018-09-10 10:20:11.312151","action":0,"type":0,"Query":"SELECT COL1,COL2\n FROM TABLE1\LIMIT 1","ID":2}
{"ts": "2018-09-10 10:20:11.312151","action":"","type":0,"Query":"","ID":2} like that
because if above is encased in brackets '[]', it's probably a JSON file.
and can easily be loaded into a dictionary with json.load
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading an ASCII text file and parsing data... oradba4u 2 2,801 Jun-08-2024, 12:41 AM
Last Post: oradba4u
Sad problems with reading csv file. MassiJames 3 3,959 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 3,249 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 3,042 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 2,659 Dec-30-2022, 10:18 AM
Last Post: ibreeden
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 2,853 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Reading Specific Rows In a CSV File finndude 3 2,590 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 2,288 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Issue in writing sql data into csv for decimal value to scientific notation mg24 8 7,245 Dec-06-2022, 11:09 AM
Last Post: mg24
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 4,382 Nov-30-2022, 10:54 PM
Last Post: Larz60+

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.