假设我的第一个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)
答案 0 :(得分: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'))
<强>输出强>
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
之后?