简体   繁体   English

很好地将a.txt文件转换为.json文件

[英]Nicely convert a .txt file to .json file

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).我有一个data.txt文件,我想将其转换为data.json文件并打印一个不错的前 2 个条目( data.txt包含 3 个唯一 ID)。

The data.txt can oublicly found here (this is a sample - original file contains 10000 unique "linkedin_internal_id ). data.txt可以在此处公开找到(这是一个示例 - 原始文件包含 10000 个唯一的"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 \\ ).此代码不会打印data.txt的适当JSON格式(它还包括\\ )。 Also, my jupyter notebook gets stacked because of the large file size, for this, I want to nicely print only the first 2 entries.此外,由于文件很大,我的jupyter notebook会堆积起来,为此,我只想很好地打印前 2 个条目。

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.它被称为新行分隔json ,其中每一行都是有效的 JSON 值行分隔符是 '\n' ,您可以像这样逐行读取它并将其推送到list ,这样以后您就可以轻松进一步迭代/处理它。 See: ldjson参见: 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])

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.这仅读取和解析数据文件的前两行,而不是加载整个内容然后提取前两项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM