Как я могу объединить данные двух файлов JSON в Python - PullRequest
0 голосов
/ 20 мая 2018

Предположим, что мои первые данные из файла 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)"}

Моя конечная цель - объединить текст файла JSON.данные только путем добавления «1» в первом файле и «0» во втором файле.

Я написал такой скрипт, но уверен, что не могу сделать это в 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)

1 Ответ

0 голосов
/ 20 мая 2018
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?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...