Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

python - json到csv逐行在python中转换(json to csv convert in python line by line)

hi need to convert a json file to csv

(您好需要将json文件转换为csv)

my code is able to get only one line but there are more that 100 lines

(我的代码只能得到一行,但是有超过100行)

here is the json data :

(这是json数据:)

{"batchId":21948391,"timestamp":"2019-11-26 12:00:25","sopVersion":"2","additionalData":{"peer39":{"signal":"UNSAFE","scope":0,"response":{"responseType":0,"language":"en","peer39Categories":{"18936":"67","18981":"67","19117":"99","19138":"60","19155":"62","19156":"63","19157":"61","19158":"64","19159":"74","19160":"61","19161":"61","19162":"61","19205":"80","19206":"80","19207":"80","19208":"80","19209":"80","19210":"80","19211":"80","19212":"80","19213":"80","19215":"80","19216":"80","22951":"80","24625":"80","38961":"98"}}},"proximic":{"signal":"SAFE","scope":"environment","response":{"scope":"environment","pageerror":"105","language":"en","brandprotection":{"rating":{"pg13":0.37,"g":0.31,"r":0.31},"safetylevel":{"platinum":0.47,"unsafe":0.25,"silver":0.19,"gold":0.04,"bronze":0.02},"safetylevel_new":{"unsafe":1},"nonstandardcontent":{"mobile_optimized":0.97,"offensive":0.22,"ugc":0.17,"negative_news":0.05,"crime_violence":0.04,"alcohol":0.02,"accidents":0.01,"nsfw":0.01}},"contextual":[{"type":"iab","category":[{"weight":100,"name":"Food & Drink","id":"IAB8"},{"weight":92,"name":"Arts & Entertainment","id":"IAB1"},{"weight":92,"name":"Food & Drink::Dining Out","id":"IAB8-9"}]},{"type":"px","category":[{"weight":100,"name":"fast-food restaurants"},{"weight":99,"name":"restaurant chains"},{"weight":98,"name":"restaurants"},{"weight":98,"name":"food and drink"},{"weight":96,"name":"foods"},{"weight":95,"name":"fast food"}]}],"trustprofile":{}}},"AAX":{"signal":"SAFE"}},"annotations":[{"date":"2019-11-26 10:30:57","reviewer":"ranganaa","responses":[{"questionId":7,"questionName":"Adult","questionText":"Does the page contain adult/suggestive content?","answers":[]},{"questionId":17,"questionName":"online_fraud","questionText":"Does the page contain Online Fraud?","answers":[]},{"questionId":14,"questionName":"terrorism","questionText":"Does the page talk about terrorism?","answers":[]},{"questionId":11,"questionName":"illegal","questionText":"Does the page contain Illegal Conduct/Service/Product?","answers":[]},{"questionId":8,"questionName":"drugs","questionText":"Does the page talk about abuse of prescription and/or illegal recreational drugs?","answers":[]},{"questionId":18,"questionName":"online_gambling","questionText":"Does the page promote Online Gambling?","answers":[]},{"questionId":15,"questionName":"violence","questionText":"Does the page contain Arms and Violence?","answers":[]},{"questionId":12,"questionName":"threatning","questionText":"Does the page contain Threatening and Harmful Content?","answers":[{"answerDisplayText":"Threatening comments","answerValue":"threatening_comments"}]},{"questionId":9,"questionName":"alcohol","questionText":"Does the page promote the use of Alcohol?","answers":[]},{"questionId":6,"questionName":"Rendered","questionText":"In which language was the page rendered?","answers":[{"answerDisplayText":"English","answerValue":"english"}]},{"questionId":19,"questionName":"sure_of_response","questionText":"Are you sure of your response?","answers":[{"answerDisplayText":"Yes","answerValue":"yes"}]},{"questionId":16,"questionName":"tragedy","questionText":"Does the page contain or make light of tragedy?","answers":[]},{"questionId":13,"questionName":"sensitive","questionText":"Does the page contain Sensitive and/or discriminatory content?","answers":[]},{"questionId":10,"questionName":"tobacco","questionText":"Does the page promote the use of Tobacco?","answers":[]}]}],"url":"https://www.oola.com/day-to-day-life/2482841/food-workers-share-the-most-terrible-customer-theyve-ever-dealt-with/?utm_source=kwy&utm_campaign=rola-kwy-m-us-7ead8e13&utm_content=2251&utm_term=rola-kwy-m-us-7ead8e13.g75zcper&kwp_0=1448353&kwp_4=4425401&kwp_1=1868176&fbclid=IwAR2ghhMelgGAGCaoNlScykFzT4cu5RHD4lcNw2RNfQs9BqVQNpl1sa-BuYA","userSubmittedIdentifier":"b488cc2f8d69fb9f015fe2a3fdde9266_20191121"}
{"batchId":21948391,"timestamp":"2019-11-26 12:00:25","sopVersion":"2","additionalData":{"peer39":{"signal":"UNSAFE","scope":2,"response":{"responseType":2,"language":"00","peer39Categories":{"19155":"62","19158":"64","38962":"88"}}},"proximic":{"signal":"UNSAFE","scope":"page","response":{"scope":"page","language":"en","brandprotection":{"rating":{"r":1},"safetylevel":{"unsafe":1},"nonstandardcontent":{"offensive":1,"ugc":1,"mobile_optimized":1}},"contextual":[{"type":"iab","category":[{"weight":100,"name":"Food & Drink","id":"IAB8"},{"weight":100,"name":"Food & Drink::Dining Out","id":"IAB8-9"},{"weight":100,"name":"Shopping","id":"IAB22"},{"weight":90,"name":"Home & Garden::Appliances","id":"IAB10-1"},{"weight":90,"name":"Home & Garden","id":"IAB10"}]},{"type":"px","category":[{"weight":80,"name":"diners"},{"weight":68,"name":"musical television series"},{"weight":68,"name":"singaporean television series"},{"weight":56,"name":"canadian restaurateurs"},{"weight":55,"name":"clothing-free events"},{"weight":47,"name":"clothing-optional events"}]}],"trustprofile":{}}},"AAX":{"signal":"UNSAFE"}},"annotations":[{"date":"2019-11-26 10:19:27","reviewer":"ranganaa","responses":[{"questionId":7,"questionName":"Adult","questionText":"Does the page contain adult/suggestive content?","answers":[]},{"questionId":17,"questionName":"online_fraud","questionText":"Does the page contain Online Fraud?","answers":[]},{"questionId":14,"questionName":"terrorism","questionText":"Does the page talk about terrorism?","answers":[]},{"questionId":11,"questionName":"illegal","questionText":"Does the page contain Illegal Conduct/Service/Product?","answers":[{"answerDisplayText":"Violent crime (Cruelty or Abuse towards Animals,Murder, Kidnapping, Robbery, Arson, Vandalism, Child Abuse)","answerValue":"violent_crime"},{"answerDisplayText":"Non Violent Crime (Theft, Cyber Bullying, Tax Evading, Forgery, Embezzelment)","answerValue":"non_violent_crime"}]},{"questionId":8,"questionName":"drugs","questionText":"Does the page talk about abuse of prescription and/or illegal recreational drugs?","answers":[]},{"questionId":18,"questionName":"online_gambling","questionText":"Does the page promote Online Gambling?","answers":[]},{"questionId":15,"questionName":"violence","questionText":"Does the page contain Arms and Violence?","answers":[]},{"questionId":12,"questionName":"threatning","questionText":"Does the page contain Threatening and Harmful Content?","answers":[{"answerDisplayText":"Threatening comments","answerValue":"threatening_comments"}]},{"questionId":9,"questionName":"alcohol","questionText":"Does the page promote the use of Alcohol?","answers":[]},{"questionId":6,"questionName":"Rendered","questionText":"In which language was the page rendered?","answers":[{"answerDisplayText":"English","answerValue":"english"}]},{"questionId":19,"questionName":"sure_of_response","questionText":"Are you sure of your response?","answers":[{"answerDisplayText":"Yes","answerValue":"yes"}]},{"questionId":16,"questionName":"tragedy","questionText":"Does the page contain or make light of tragedy?","answers":[]},{"questionId":13,"questionName":"sensitive","questionText":"Does the page contain Sensitive and/or discriminatory content?","answers":[{"answerDisplayText":"Biased Content (Gender, race,political, religion,sexual orientation, body shaming, disability)","answerValue":"biased_content"}]},{"questionId":10,"questionName":"tobacco","questionText":"Does the page promote the use of Tobacco?","answers":[]}]}],"url":"https://www.oola.com/life-in-flavor/2479120/diners-share-the-biggest-server-meltdown-theyve-ever-witnessed-while-eating-out","userSubmittedIdentifier":"b5feaafdfc52e2d066d03a7ec30dffec_20191121"}
{"batchId":21948391,"timestamp":"2019-11-26 12:00:25","sopVersion":"2","additionalData":{"peer39":{"signal":"UNSAFE","scope":2,"response":{"responseType":2,"language":"00","peer39Categories":{"19116":"90","19158":"64","27266":"90"}}},"proximic":{"signal":"SAFE","scope":"environment","response":{"scope":"environment","pageerror":"105","language":"en","brandprotection":{"rating":{"g":0.61,"pg13":0.21,"r":0.17},"safetylevel":{"platinum":0.68,"unsafe":0.12,"bronze":0.11,"gold":0.03,"silver":0.02},"safetylevel_new":{"gold":1},"nonstandardcontent":{"mobile_optimized":1,"crime_violence":0.12,"negative_news":0.11,"offensive":0.11,"ugc":0.06,"nsfw":0.04,"terrorism":0.02,"weapons":0.01,"health_issues":0.01}},"contextual":[{"type":"iab","category":[{"weight":100,"name":"Law, Gov't & Politics","id":"IAB11"},{"weight":99,"name":"Business","id":"IAB3"},{"weight":96,"name":"Business::Government","id":"IAB3-7"},{"weight":95,"name":"Arts & Entertainment","id":"IAB1"},{"weight":93,"name":"News","id":"IAB12"}]},{"type":"px","category":[{"weight":100,"name":"politics"},{"weight":99,"name":"presidency of donald trump"},{"weight":95,"name":"impeachment"},{"weight":94,"name":"donald trump"},{"weight":94,"name":"BmFI"},{"weight":93,"name":"joe biden"}]}],"trustprofile":{}}},"AAX":{"signal":"SAFE"}},"annotations":[{"date":"2019-11-26 07:24:54","reviewer":"akochumo","responses":[{"questionId":7,"questionName":"Adult","questionText":"Does the page contain adult/suggestive content?","answers":[]},{"questionId":17,"questionName":"online_fraud","questionText":"Does the page contain Online Fraud?","answers":[]},{"questionId":14,"questionName":"terrorism","questionText":"Does the page talk about terrorism?","answers":[]},{"questionId":11,"questionName":"illegal","questionText":"Does the page contain Illegal Conduct/Service/Product?","answers":[]},{"questionId":8,"questionName":"drugs","questionText":"Does the page talk about abuse of prescription and/or illegal recreational drugs?","answers":[]},{"questionId":18,"questionName":"online_gambling","questionText":"Does the page promote Online Gambling?","answers":[]},{"questionId":15,"questionName":"violence","questionText":"Does the page contain Arms and Violence?","answers":[]},{"questionId":12,"questionName":"threatning","questionText":"Does the page contain Threatening and Harmful Content?","answers":[]},{"questionId":9,"questionName":"alcohol","questionText":"Does the page promote the use of Alcohol?","answers":[]},{"questionId":6,"questionName":"Rendered","questionText":"In which language was the page rendered?","answers":[{"answerDisplayText":"Other","answerValue":"other"}]},{"questionId":19,"questionName":"sure_of_response","questionText":"Are you sure of your response?","answers":[{"answerDisplayText":"Yes","answerValue":"yes"}]},{"questionId":16,"questionName":"tragedy","questionText":"Does the page contain or make light of tragedy?","answers":[]},{"questionId":13,"questionName":"sensitive","questionText":"Does the page contain Sensitive and/or discriminatory content?","answers":[]},{"questionId":10,"questionName":"tobacco","questionText":"Does the page promote the use of Tobacco?","answers":[]}]}],"url":"https://www.chicksonright.com/blog/2019/11/12/sprite-is-selling-more-than-soda-and-im-not-buying-any-of-it","userSubmittedIdentifier":"d4e9499c09046743bf18b2cae51c72c3_20191121"}

here is my code

(这是我的代码)

import csv
import json
with open('try') as infile:
jsdata = json.loads(infile.read())
with open("test.csv", "w") as outfile:
    f = csv.writer(outfile)
    f.writerow(["batchId","url","peer39","proximic","AAX","reviewer","Adult","online_fraud","terrorism",
            "illegal","drugs","online_gambling","violence","threatning","alcohol","Rendered",
            "answerDisplayText","sure_of_response","tragedy",
            "sensitive","tobacco"])
    f.writerow([jsdata["batchId"],
            jsdata["url"],
            jsdata["additionalData"]["peer39"]["signal"],
         

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...