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.
CodePudding user response:
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])
CodePudding user response:
Something like this?
import json
with open('data.txt', 'r') as f:
data = [json.loads(f.readline()) for i in range(2)]
print(json.dumps(data))
This only reads and parses the first two lines of the data file, instead of loading the whole thing and then extracting the first two items.