首页 > 解决方案 > 如何在 Python 中合并两个 json 文件数据

问题描述

假设我的第一个 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 "}

第二个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)"}

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

我写了这样的脚本,但我确定我不能在 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

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

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)

标签: pythonjsonparsing

解决方案


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'))

输出

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)'

输出是

0
1
0
1

或者你想成为第一1和之后0


推荐阅读