简体   繁体   English

尝试将大 tsv 文件转换为 json

[英]Trying to convert a big tsv file to json

I've a tsv file, which I need to convert it into a json file.我有一个 tsv 文件,我需要将其转换为 json 文件。 I'm using this python script which is exporting a empty json file.我正在使用这个 python 脚本,它正在导出一个空的 json 文件。

import json
data={}
with open('data.json', 'w') as outfile,open("data.tsv","r") as f:
for line in f:
   sp=line.split()
   data.setdefault("data",[])
json.dump(data, outfile)

This can be done by pandas , but am not sure about performance 这可以通过熊猫来完成,但是不确定性能

df.to_json df.to_json

 df = pd.read_csv('data.tsv',sep='\t') # read your tsv file 
 df.to_json('data.json') #save it as json . refer orient='values' or 'columns' as per your requirements 

You never use the sp in your code. 您永远不会在代码中使用sp

To properly convert the tsv, you should read the first line separately, to get the "column names", then read the following lines and populate a list of dictionaries. 为了正确地转换tsv,您应该分别阅读第一行以获得“列名”,然后阅读以下各行并填充字典列表。

Here's what your code should look like: 这是您的代码应如下所示:

import json
data=[{}]
with open('data.json', 'w') as outfile, open("data.tsv","r") as f:
firstline = f.readline()
columns = firstline.split()
lines = f.readlines()[1:]
for line in lines:
    values = line.split()
    entry = dict(zip(columns, values))
    data.append(entry)
json.dump(data, outfile)

This will output a file containing a list of tsv rows as objects. 这将输出一个包含tsv行列表的文件作为对象。

Nowadays, it is very simple to solve problems in this way.如今,以这种方式解决问题非常简单。

You can try https://toolsofweb.com/tsv-to-json for TSV to JSON您可以尝试https://toolsofweb.com/tsv-to-json用于 TSV 到 JSON

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

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