Issue
I have a data.txt
file which I want to convert to a data.json
file and print a nice first 2 entries (data.txt
contains 3 unique IDs).
The data.txt
can oublicly found here (this is a sample - original file contains 10000 unique "linkedin_internal_id
).
I tried the following:
with open("data.txt", "r") as f:
content = f.read()
data = json.dumps(content, indent=3)
This code doesn't print the appropriate JSON
format of data.txt
(it also includes \\
). Also, my jupyter notebook
gets stacked because of the large file size, for this, I want to nicely print only the first 2 entries.
Solution
It is called new line delimited json
where each line is a valid JSON value and the line separator is '\n', you can read it like this line by line and push it to a list
, so later it will be easy for you to iterate/process it further. See: ldjson
import json
with open("data.txt", "r") as f:
contents = f.read()
data = [json.loads(item) for item in contents.strip().split('\n')]
print(data[0:2])
Answered By - Always Sunny
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.