我有一个JSONObject,它需要与标头一起以CSV文件编写。
我的错误:
` File "/usr/local/Cellar/python/3.7.5/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py", line 341, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not list`
我的JSONObject(updatedRecord):
[
"{\"name\": \"Anu Ghosh\", \"email\": \"abcd@gmail.com\", \"mobile_number\": \"99999999\", \"skills\": [\"Recruitment\", \"International\", \"Coding\", \"Wifi\", \"Programming\", \"Training\", \"Mobile\", \"Sourcing\", \"Email\", \"English\", \"Design\", \"Electronics\", \"System\", \"Strategy\", \"Content\", \"Scheduling\", \"Technical\", \"Hardware\", \"Lighting\", \"C\"], \"college_name\": null, \"degree\": [\"Bachelor of Technology in \\nElectronics and \\nTelecommunication(B.Tech)\"], \"designation\": [\"Internet of Things (IOT)\"], \"experience\": \"1\", \"company_names\": null, \"no_of_pages\": 2, \"total_experience\": 0, \"fullName\": \"Anu Ghosh\", \"emailId\": \"abcd@gmail.com\", \"phoneNumber\": \"23454654434\", \"currentLocation\": \"Mumbai\", \"closestCity\": \"Mumbai\", \"resumeLink\": \"/Users/abc/Downloads/aaa.pdf\", \"applicationJobCode\": \"DEF\", \"applicationJobTitle\": \"HR\"}",
"{\"name\": \"Prakash\", \"email\": \"sdsn@gmail.com\", \"mobile_number\": \"+99877678888\", \"skills\": [\"Scala\", \"Google drive\", \"Database\", \"Rest\", \"Communication\", \"Zookeeper\", \"Programming\", \"Sql\", \"Python\", \"Agile\", \"Javascript\", \"Mobile\", \"Mysql\", \"Json\", \"Compliance\", \"Hotels\", \"Fabric\", \"Architecture\", \"Api\", \"Requests\", \"Reporting\", \"Operations\", \"Pycharm\", \"Troubleshooting\", \"Html\", \"Ui\", \"Video\", \"C++\", \"English\", \"Gap analysis\", \"Java\", \"Cloud\", \"Design\", \"Windows\", \"Visual\", \"Click\", \"Oracle\", \"Jira\", \"Scrum\", \"System\", \"Hadoop\", \"Security\", \"Schedules\", \"Ibm\", \"Spark\", \"Analysis\", \"Linux\", \"Project planning\", \"Intranet\", \"Postgresql\", \"Css\", \"Process\", \"Software development life cycle\", \"Technical\", \"Js\", \"Aws\", \"Sdlc\", \"C\", \"Certification\", \"Test cases\"], \"college_name\": null, \"degree\": [\"M.S (IT)\"], \"designation\": null, \"experience\": \"4\", \"company_names\": [\"Oracle\"], \"no_of_pages\": 7, \"total_experience\": 0.0, \"fullName\": \" prakash\", \"emailId\": \"bbbb@gmail.com\", \"phoneNumber\": \"99998898998\", \"currentLocation\": \"Mumbai\", \"closestCity\": \"Mumbai\", \"resumeLink\": \"/Users/abc/Downloads/v_cv.pdf\", \"applicationJobCode\": \"ABVC\", \"applicationJobTitle\": \"Developer\"}"
]
我已经写了这个无效的代码块。
def writeToCSV(updatedDBRecord):
record = json.dumps(updatedDBRecord)
record = json.loads(record)
f = csv.writer(open('/Users/varunprakash/Documents/candidate.csv','w'))
f.writerow(['Name','EmailId','PhoneNumber','Experience','Skill','CurrentLocation','ClosestCity','ResumeLink','ApplicationJobCode','ApplicationJobTitle'])
for data in record:
print(data)
f.writerow(data['fullName'],data['emailId'],data['phoneNumber'],data['experience'],data['skill'],data['currentLocation'],data['closestCity'],data['resumeLink'],data['applicationJobCode'],data['applicationJobTitle'])
f.close()
任何想法都值得赞赏。
答案 0 :(得分:1)
起初您弄乱了装载和转储,但是您的逻辑是合理的。
import csv
import json
from pprint import pprint
def writeToCSV(updatedDBRecord):
for data in updatedDBRecord:
record = json.loads(data)
with open(r'c:\tmp\tmp.csv','w') as fp:
f = csv.writer(fp)
f.writerow(['Name','EmailId','PhoneNumber','Experience','Skills','CurrentLocation','ClosestCity','ResumeLink','ApplicationJobCode','ApplicationJobTitle'])
f.writerow([record['fullName'],record['emailId'],record['phoneNumber'],record['experience'],record['skills'],record['currentLocation'],record['closestCity'],record['resumeLink'],record['applicationJobCode'],record['applicationJobTitle']])
data = [
"{\"name\": \"Anu Ghosh\", \"email\": \"abcd@gmail.com\", \"mobile_number\": \"99999999\", \"skills\": [\"Recruitment\", \"International\", \"Coding\", \"Wifi\", \"Programming\", \"Training\", \"Mobile\", \"Sourcing\", \"Email\", \"English\", \"Design\", \"Electronics\", \"System\", \"Strategy\", \"Content\", \"Scheduling\", \"Technical\", \"Hardware\", \"Lighting\", \"C\"], \"college_name\": null, \"degree\": [\"Bachelor of Technology in \\nElectronics and \\nTelecommunication(B.Tech)\"], \"designation\": [\"Internet of Things (IOT)\"], \"experience\": \"1\", \"company_names\": null, \"no_of_pages\": 2, \"total_experience\": 0, \"fullName\": \"Anu Ghosh\", \"emailId\": \"abcd@gmail.com\", \"phoneNumber\": \"23454654434\", \"currentLocation\": \"Mumbai\", \"closestCity\": \"Mumbai\", \"resumeLink\": \"/Users/abc/Downloads/aaa.pdf\", \"applicationJobCode\": \"DEF\", \"applicationJobTitle\": \"HR\"}",
"{\"name\": \"Prakash\", \"email\": \"sdsn@gmail.com\", \"mobile_number\": \"+99877678888\", \"skills\": [\"Scala\", \"Google drive\", \"Database\", \"Rest\", \"Communication\", \"Zookeeper\", \"Programming\", \"Sql\", \"Python\", \"Agile\", \"Javascript\", \"Mobile\", \"Mysql\", \"Json\", \"Compliance\", \"Hotels\", \"Fabric\", \"Architecture\", \"Api\", \"Requests\", \"Reporting\", \"Operations\", \"Pycharm\", \"Troubleshooting\", \"Html\", \"Ui\", \"Video\", \"C++\", \"English\", \"Gap analysis\", \"Java\", \"Cloud\", \"Design\", \"Windows\", \"Visual\", \"Click\", \"Oracle\", \"Jira\", \"Scrum\", \"System\", \"Hadoop\", \"Security\", \"Schedules\", \"Ibm\", \"Spark\", \"Analysis\", \"Linux\", \"Project planning\", \"Intranet\", \"Postgresql\", \"Css\", \"Process\", \"Software development life cycle\", \"Technical\", \"Js\", \"Aws\", \"Sdlc\", \"C\", \"Certification\", \"Test cases\"], \"college_name\": null, \"degree\": [\"M.S (IT)\"], \"designation\": null, \"experience\": \"4\", \"company_names\": [\"Oracle\"], \"no_of_pages\": 7, \"total_experience\": 0.0, \"fullName\": \" prakash\", \"emailId\": \"bbbb@gmail.com\", \"phoneNumber\": \"99998898998\", \"currentLocation\": \"Mumbai\", \"closestCity\": \"Mumbai\", \"resumeLink\": \"/Users/abc/Downloads/v_cv.pdf\", \"applicationJobCode\": \"ABVC\", \"applicationJobTitle\": \"Developer\"}"
]
writeToCSV(data)
这按预期工作。