Mam JSONObject, który musi być zapisany w plikach CSV wraz z nagłówkami.

Mój błąd:

` 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`

Mój 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\"}"
]

Napisałem ten blok kodu, który nie działa.

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

Wszelkie pomysły są mile widziane.

0
Maverick 18 listopad 2019, 19:27
1
Pokazany przez Ciebie przykład nie zgadza się z Twoim kodem. W każdym razie komunikat o błędzie sugeruje, że próbujesz napisać np. pole skills jako pojedynczy ciąg; ale to nie jest ciąg, to lista ciągów. Próba zapisania ustrukturyzowanych danych w CSV to powszechny problem dla początkujących — CSV nie ma żadnych zagnieżdżonych struktur, więc musisz jakoś serializować dane (co jest brzydkie i kruche) lub po prostu wybrać lepszy format wyjściowy, który jest zgodny z Twoimi danymi Model.
 – 
tripleee
18 listopad 2019, 19:43

2 odpowiedzi

Na początku zepsułeś obciążenia i zrzuty, ale twoja logika jest rozsądna.

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)

Działa to zgodnie z oczekiwaniami.

1
Fakher Mokadem 18 listopad 2019, 20:00

Format json jest nieprawidłowy.

Próbka JSON:

[
 {"name": "Anu Ghosh", "email": "abcd@gmail.com"},
 {"name": "Prakash", "email": "sdsn@gmail.com"}
]
0
Shiva Bala Rakavi 18 listopad 2019, 19:45