如何在Python中合并两个json文件数据

时间:2023-01-15 12:41:00

Suppose My first json file data is like this.

假设我的第一个json文件数据是这样的。

{"derivedFrom": "1e21781bfc33ae80e074369165368080", "text": "PAKIS RAPE KIDS: Mexico police in college raids: Vehicles are set ablaze and more than 120 peo... @Ewok_League #edl"}
{"derivedFrom": "1e26ebbfbfaea600e0746a3016b628a8", "text": "Bullfighting sparks animal rights protests in Mexico - video - The Guardian "}

and second JSON file data is like this

和第二个JSON文件数据是这样的

{"derivedFrom": "1e21292e9b4ca680e074bd999ef8cc3a","text": "@TheArkham No crea que me olvido de los amigos,espero todo este marchando bien, un abrazo."}
{"derivedFrom": "1e2602635130a980e0744bad1c470046", "text": "Avisale al IFE Que #SiDragonBallFueraMexicano Le hubiera dado a Noe Hernandez Semillas Del Ermita\u00f1o (ESO HUBIERA SIDO ESTUPENDO. QEPD)"}

My ultimate goal is to merge both the JSON file's text data only by appending "1," in the first file and "0," in the second file.

我的最终目标是仅在第一个文件中附加“1”并在第二个文件中附加“0”来合并JSON文件的文本数据。

I wrote the script like this but I'm sure I cannot do in this way in Python.

我写了这样的脚本,但我确信我不能用Python这样做。

import json

positiveFile = open('train_posi_tweets_2017.txt')
negativeFile = open('train_nega_tweets_2017.txt')

for linePos,lineNeg in positiveFile,negativeFile:
    distros_dictPos=json.loads(linePos)
    distros_dictNeg=json.loads(lineNeg)
    distros_dictPosVal = distros_dict['text'].encode('utf-8')
    distros_dictNegVal = distros_dict['text'].encode('utf-8')
    print distros_dictNegVal

So the final output should be like this.

所以最终输出应该是这样的。

1,"PAKIS RAPE KIDS: Mexico police in college raids: Vehicles are set ablaze and more than 120 peo... @Ewok_League #edl"
1,"Bullfighting sparks animal rights protests in Mexico - video - The Guardian "
0,"@TheArkham No crea que me olvido de los amigos,espero todo este marchando bien, un abrazo."
0,"Avisale al IFE Que #SiDragonBallFueraMexicano Le hubiera dado a Noe Hernandez Semillas Del Ermita\u00f1o (ESO HUBIERA SIDO ESTUPENDO. QEPD)

1 个解决方案

#1


0  

import json

positiveFile = open('test1.txt')
negativeFile = open('test2.txt')

for linePos,lineNeg in positiveFile,negativeFile:                                                                                                                                 
    print(1, json.loads(linePos)['text'].encode('utf-8'))
    print(0, json.loads(lineNeg)['text'].encode('utf-8'))

Output

1 b'PAKIS RAPE KIDS: Mexico police in college raids: Vehicles are set ablaze and more than 120 peo... @Ewok_League #edl'
0 b'Bullfighting sparks animal rights protests in Mexico - video - The Guardian '
1 b'@TheArkham No crea que me olvido de los amigos,espero todo este marchando bien, un abrazo.'
0 b'Avisale al IFE Que #SiDragonBallFueraMexicano Le hubiera dado a Noe Hernandez Semillas Del Ermita\xc3\xb1o (ESO HUBIERA SIDO ESTUPENDO. QEPD)'

the output is

输出是

0
1
0
1

or you want to be first of all 1 and after this 0 ?

或者你想成为第一个并且在这之后0?

#1


0  

import json

positiveFile = open('test1.txt')
negativeFile = open('test2.txt')

for linePos,lineNeg in positiveFile,negativeFile:                                                                                                                                 
    print(1, json.loads(linePos)['text'].encode('utf-8'))
    print(0, json.loads(lineNeg)['text'].encode('utf-8'))

Output

1 b'PAKIS RAPE KIDS: Mexico police in college raids: Vehicles are set ablaze and more than 120 peo... @Ewok_League #edl'
0 b'Bullfighting sparks animal rights protests in Mexico - video - The Guardian '
1 b'@TheArkham No crea que me olvido de los amigos,espero todo este marchando bien, un abrazo.'
0 b'Avisale al IFE Que #SiDragonBallFueraMexicano Le hubiera dado a Noe Hernandez Semillas Del Ermita\xc3\xb1o (ESO HUBIERA SIDO ESTUPENDO. QEPD)'

the output is

输出是

0
1
0
1

or you want to be first of all 1 and after this 0 ?

或者你想成为第一个并且在这之后0?