Python Uconvert json对CSV文件的响应

Python Uconvert json对CSV文件的响应,python,json,csv,Python,Json,Csv,我有下面的json响应 {"userId":"vendor","sessionId":"3d716be43d094fefa2261b5347a6127f","channelName":"/omsevents/usession/3d716be43d094fefa2261b5347a6127f","fadList":["NERemoteConfig","ShowSWStatus","esm_modify_erp","ManageStaticLink","ForceActivateSw","GetNe

我有下面的json响应

{"userId":"vendor","sessionId":"3d716be43d094fefa2261b5347a6127f","channelName":"/omsevents/usession/3d716be43d094fefa2261b5347a6127f","fadList":["NERemoteConfig","ShowSWStatus","esm_modify_erp","ManageStaticLink","ForceActivateSw","GetNeTime","AlarmDebounceModify","SetMeTime","DeleteME","esm_prov_tunnel","CreateJob","GlobalAlignDownwards","esm_prov_mclag","removeSubnet","MapNodeMovement","ModifyRNEIndex","ViewSwdlJobReport","GetSwPkg","AdvancedConstruct","wdm_performanceManagementFunction","Map_FUNCTION_admin_operator","ManageNEInventory","GetNeList","OSAddressRW","ShowInformations","SwStatusSwdlJob","CreateSwdlSession","RunPerfMon_15m","cpb_operationFunction","AbortSwdlSession","Cmd_MapInv_SbnListNodes","F_DELETE_CUSTOMER_TEMPLATE","esm_prov_npa","InternalCmd_MapView_View_ZoomIn","OSAddressRO","Cmd_TM_FUNCTION_allUser","esm_modify_lag","AlignUpwards","DeleteSwdlNes","InternalCmd_MapView_Obj_Undo","ShowBkupSession","CreateDynamicLink","ViewRestoreJobReport","DisplayRI","PathBuild","ManageMonitoring","ShowRestoreNes","ManageMeTime","SysMonRead","DeleteBkpNes","DeleteSwdlSession","RemoveGeneric","ShowSwdlNes","ManageNTP","Cmd_CLOG_Admin_Header_P","ShowEqp","DeleteBkpSession","DeleteRestoreSession","Cmd_CLOG_Admin_Header","ManageNEPopulation","GetSwStatusByType","ShowIsaSWStatus","MEAddressRW","ActivateSw","esm_generate_pm_report","Cmd_Inv_LinkListConnections","Cmd_MapNav_ObjPtrToNPAview","Cmd_CLOG_Ack","MSGUI_FUNCTION_ADMIN_TOPOLOGICALVIEWS","PingNE","esm_modify_mclag","AbortJob","ManualAlignDownwards","Cmd_MapInv_ListTopLevelNtws_PHY_TOPVIEW","Cmd_MapInv_SbnListChildSbns","ShowRestoreSession","Cmd_MapAct_SbnRemoveNode","F_ADD_CUSTOMER_TEMPLATE","ManageAccess","CreateBkupSessionWizard","CreateRestoreSessionWizard","ShowJobList","MigrateME","OpenJobWizard","Cmd_TM_FUNCTION_admin_construct","SysMonAdmin","ModifyLocation","wdm_neManagementFunction","OpenGetSwByTypeWizard","ModifyME","Cmd_MapNav_OpcToMap","NavigationToELM","Cmd_MapAct_SbnDeleteSbnAndSubordinates","AbortNe","Cmd_CLOG_User_Header","MultipleNeDownload","Cmd_TM_FUNCTION_admin_construct_operator","Admin","AddSwdlNes","saveSubnet","wdm_legacyManagementFunction","GetSwdl","Cmd_MapInv_SbnCreateMap","Cmd_MapAct_SbnCommitMapModify","Cmd_MapInv_ListTopLevelNtws_PHY_Test","esm_prov_customer","physconnInNet","UploadRemoteInventory","GetSwDetail","ActiveJob","NEInventory","ManageResynchro","RunPerfMon_hour","Operations","Cmd_MapWiz_ModifySubnet","AbortBkupSession","GetNeLabel","SetNeTime","InternalCmd_MapView_View_ZoomOut","NavigationToEquipments","ManageFilter","ViewBkupJobReport","DeleteSw","wdm_inventoryFunction","ManageLocalAccess","NetworkAddressRW","esm_job_reschedule","AddBkpNes","SysMonView","esm_design_template","Cmd_MapInv_PopulateSbnsInTree_PHY","addSubnet","InternalCmd_MapView_Obj_Redo","Cmd_MapInv_ShowOpcView","NetworkAddressRO","esm_design_feature","InternalCmd_MapView_View_ZoomBestFit","AlarmDebounceView","EditSwdlJobWizard","CreatePartialSwdlJob","ModifyACD","NavigationToURLs","EMLInventory","viewMap","Cmd_TM_FUNCTION_admin","esm_prov_service","Cmd_MapAct_SbnAddNode","DeleteRestoreNes","ActiveSoftware","NavigationToHostNe","AdvancedViewer","wdm_alarmManagementFunction","AllUsers","CreateSUBNE","ShowSwdlSession","EditJob","ManageSupervision","CreateNE","esm_modify_tunnel","ModifyUserLabel","Cmd_MapWiz_SbnCreateSimple","esm_modify_npa","GetNeType","Cmd_MapAct_DataSynchronize","OpenGetSwByNameWizard","InternalCmd_MapView_View_ChangeBackground","ResynchroAll","RunPerfMon_day","MulitpleNeBackup","ModifyComments","CreatePartialSwdlJobFromNeList","F_MODIFY_CUSTOMER_TEMPLATE","ManageLinkInventory","GetFtServer","esm_modify_service","ManageMib","EditRestorJobWizard","esm_deploy_networkConfig","Cmd_EventParameter","ShowJobStatus","ShowStatuses","addNode","Cmd_CLOG_User_Header_P","NavigationToAlarms","InternalCmd_MapView_Obj_Save","EditBkupJobWizard","InternalCmd_MapView_View_SwitchLayer","CreateRestoreSession","esm_job_delete","esm_prov_erp","Cmd_MapCrt_SbnCreate","Cmd_MapInv_ListObjsForCreateSbn","GetSwStatusByName","CreateSwdlSessionWizard","DirInventory","removeNode","ManageAdmin","esm_prov_lag","DeleteJob","CreateBkupSession","wdm_provisionFunction","ManageClone","modifySubnet","CommitSw","CreateME","ShowAlarms","InternalCmd_MapView_View_NormalSize","CreateRNE","InternalCmd_MapView_View_Miniature","ShowBackupNes","Construct","Cmd_Inv_SbnListPhyconnections","NERemoteConfig","ShowSWStatus","esm_modify_erp","ManageStaticLink","ForceActivateSw","GetNeTime","AlarmDebounceModify","SetMeTime","DeleteME","esm_prov_tunnel","CreateJob","GlobalAlignDownwards","esm_prov_mclag","removeSubnet","MapNodeMovement","ModifyRNEIndex","ViewSwdlJobReport","GetSwPkg","AdvancedConstruct","wdm_performanceManagementFunction","Map_FUNCTION_admin_operator","ManageNEInventory","GetNeList","OSAddressRW","ShowInformations","SwStatusSwdlJob","CreateSwdlSession","RunPerfMon_15m","cpb_operationFunction","AbortSwdlSession","Cmd_MapInv_SbnListNodes","F_DELETE_CUSTOMER_TEMPLATE","esm_prov_npa","InternalCmd_MapView_View_ZoomIn","OSAddressRO","Cmd_TM_FUNCTION_allUser","esm_modify_lag","AlignUpwards","DeleteSwdlNes","InternalCmd_MapView_Obj_Undo","ShowBkupSession","CreateDynamicLink","ViewRestoreJobReport","DisplayRI","PathBuild","ManageMonitoring","ShowRestoreNes","ManageMeTime","SysMonRead","DeleteBkpNes","DeleteSwdlSession","RemoveGeneric","ShowSwdlNes","ManageNTP","Cmd_CLOG_Admin_Header_P","ShowEqp","DeleteBkpSession","DeleteRestoreSession","Cmd_CLOG_Admin_Header","ManageNEPopulation","GetSwStatusByType","ShowIsaSWStatus","MEAddressRW","ActivateSw","esm_generate_pm_report","Cmd_Inv_LinkListConnections","Cmd_MapNav_ObjPtrToNPAview","Cmd_CLOG_Ack","MSGUI_FUNCTION_ADMIN_TOPOLOGICALVIEWS","PingNE","esm_modify_mclag","AbortJob","ManualAlignDownwards","Cmd_MapInv_ListTopLevelNtws_PHY_TOPVIEW","Cmd_MapInv_SbnListChildSbns","ShowRestoreSession","Cmd_MapAct_SbnRemoveNode","F_ADD_CUSTOMER_TEMPLATE","ManageAccess","CreateBkupSessionWizard","CreateRestoreSessionWizard","ShowJobList","MigrateME","OpenJobWizard","Cmd_TM_FUNCTION_admin_construct","SysMonAdmin","ModifyLocation","wdm_neManagementFunction","OpenGetSwByTypeWizard","ModifyME","Cmd_MapNav_OpcToMap","NavigationToELM","Cmd_MapAct_SbnDeleteSbnAndSubordinates","AbortNe","Cmd_CLOG_User_Header","MultipleNeDownload","Cmd_TM_FUNCTION_admin_construct_operator","Admin","AddSwdlNes","saveSubnet","wdm_legacyManagementFunction","GetSwdl","Cmd_MapInv_SbnCreateMap","Cmd_MapAct_SbnCommitMapModify","Cmd_MapInv_ListTopLevelNtws_PHY_Test","esm_prov_customer","physconnInNet","UploadRemoteInventory","GetSwDetail","ActiveJob","NEInventory","ManageResynchro","RunPerfMon_hour","Operations","Cmd_MapWiz_ModifySubnet","AbortBkupSession","GetNeLabel","SetNeTime","InternalCmd_MapView_View_ZoomOut","NavigationToEquipments","ManageFilter","ViewBkupJobReport","DeleteSw","wdm_inventoryFunction","ManageLocalAccess","NetworkAddressRW","esm_job_reschedule","AddBkpNes","SysMonView","esm_design_template","Cmd_MapInv_PopulateSbnsInTree_PHY","addSubnet","InternalCmd_MapView_Obj_Redo","Cmd_MapInv_ShowOpcView","NetworkAddressRO","esm_design_feature","InternalCmd_MapView_View_ZoomBestFit","AlarmDebounceView","EditSwdlJobWizard","CreatePartialSwdlJob","ModifyACD","NavigationToURLs","EMLInventory","viewMap","Cmd_TM_FUNCTION_admin","esm_prov_service","Cmd_MapAct_SbnAddNode","DeleteRestoreNes","ActiveSoftware","NavigationToHostNe","AdvancedViewer","wdm_alarmManagementFunction","AllUsers","CreateSUBNE","ShowSwdlSession","EditJob","ManageSupervision","CreateNE","esm_modify_tunnel","ModifyUserLabel","Cmd_MapWiz_SbnCreateSimple","esm_modify_npa","GetNeType","Cmd_MapAct_DataSynchronize","OpenGetSwByNameWizard","InternalCmd_MapView_View_ChangeBackground","ResynchroAll","RunPerfMon_day","MulitpleNeBackup","ModifyComments","CreatePartialSwdlJobFromNeList","F_MODIFY_CUSTOMER_TEMPLATE","ManageLinkInventory","GetFtServer","esm_modify_service","ManageMib","EditRestorJobWizard","esm_deploy_networkConfig","Cmd_EventParameter","ShowJobStatus","ShowStatuses","addNode","Cmd_CLOG_User_Header_P","NavigationToAlarms","InternalCmd_MapView_Obj_Save","EditBkupJobWizard","InternalCmd_MapView_View_SwitchLayer","CreateRestoreSession","esm_job_delete","esm_prov_erp","Cmd_MapCrt_SbnCreate","Cmd_MapInv_ListObjsForCreateSbn","GetSwStatusByName","CreateSwdlSessionWizard","DirInventory","removeNode","ManageAdmin","esm_prov_lag","DeleteJob","CreateBkupSession","wdm_provisionFunction","ManageClone","modifySubnet","CommitSw","CreateME","ShowAlarms","InternalCmd_MapView_View_NormalSize","CreateRNE","InternalCmd_MapView_View_Miniature","ShowBackupNes","Construct","Cmd_Inv_SbnListPhyconnections"],"nadString":"Voda unknown","userNadRole":"GLOBAL"}
我需要将其转换为csv文件,每行都有标题,下面是值。像这样

userId    sessionId    channelName            fadList   NEremote  ...
vendor     3d716be..   /omsevents/usession     so on     so on    ... 
我尝试了这个解决方案,但没有成功

def json_csv() :

    file = input("Please Enter new CSV file new  :")
    # Opening JSON file and loading the data
    # into the variable data
    with open(r"D:\json.txt") as json_file:
        data = json.load(json_file)

    employee_data = data

    # now we will open a file for writing
    data_file = open(file,'w')

    # create the csv writer object
    csv_writer = csv.writer(data_file)

    # Counter variable used for writing
    # headers to the CSV file
    count = 0

    for emp in data:
        if count == 0 :

            # Writing headers of CSV file
            header = emp.keys()
            csv_writer.writerow(header)
            count += 1

        # Writing data of CSV file
        csv_writer.writerow(emp.values())

    data_file.close()
json_csv()
更新1:

感谢Hozafya,他提供了以下解决方案:

from pandas.io.json import json_normalize
x = open("test.txt").readline()
df = json_normalize(x)
df.to_csv("file.csv")
该脚本适用于前3项。从列表开始,将所有数据放在一列中。因此,我需要数据如下,例如:

更新2: Hozayfa提供的解决方案解决了我的问题。还有最后一件事,当我直接附加json数据时,脚本工作正常,如下所示:

x = {"userId":"vendor","sessionId":"3d716be43d094fefa2261b5347a6127f","channelName":"/omsevents/usession/3d716be43d094fefa2261b5347a6127f","fadList":["NERemoteConfig","ShowSWStatus","esm_modify_erp","ManageStaticLink","ForceActivateSw","GetNeTime","AlarmDebounceModify","SetMeTime","DeleteME","esm_prov_tunnel","CreateJob","GlobalAlignDownwards","esm_prov_mclag","removeSubnet","MapNodeMovement","ModifyRNEIndex","ViewSwdlJobReport","GetSwPkg","AdvancedConstruct","wdm_performanceManagementFunction","Map_FUNCTION_admin_operator","ManageNEInventory","GetNeList","OSAddressRW","ShowInformations","SwStatusSwdlJob","CreateSwdlSession","RunPerfMon_15m","cpb_operationFunction","AbortSwdlSession","Cmd_MapInv_SbnListNodes","F_DELETE_CUSTOMER_TEMPLATE","esm_prov_npa","InternalCmd_MapView_View_ZoomIn","OSAddressRO","Cmd_TM_FUNCTION_allUser","esm_modify_lag","AlignUpwards","DeleteSwdlNes","InternalCmd_MapView_Obj_Undo","ShowBkupSession","CreateDynamicLink","ViewRestoreJobReport","DisplayRI","PathBuild","ManageMonitoring","ShowRestoreNes","ManageMeTime","SysMonRead","DeleteBkpNes","DeleteSwdlSession","RemoveGeneric","ShowSwdlNes","ManageNTP","Cmd_CLOG_Admin_Header_P","ShowEqp","DeleteBkpSession","DeleteRestoreSession","Cmd_CLOG_Admin_Header","ManageNEPopulation","GetSwStatusByType","ShowIsaSWStatus","MEAddressRW","ActivateSw","esm_generate_pm_report","Cmd_Inv_LinkListConnections","Cmd_MapNav_ObjPtrToNPAview","Cmd_CLOG_Ack","MSGUI_FUNCTION_ADMIN_TOPOLOGICALVIEWS","PingNE","esm_modify_mclag","AbortJob","ManualAlignDownwards","Cmd_MapInv_ListTopLevelNtws_PHY_TOPVIEW","Cmd_MapInv_SbnListChildSbns","ShowRestoreSession","Cmd_MapAct_SbnRemoveNode","F_ADD_CUSTOMER_TEMPLATE","ManageAccess","CreateBkupSessionWizard","CreateRestoreSessionWizard","ShowJobList","MigrateME","OpenJobWizard","Cmd_TM_FUNCTION_admin_construct","SysMonAdmin","ModifyLocation","wdm_neManagementFunction","OpenGetSwByTypeWizard","ModifyME","Cmd_MapNav_OpcToMap","NavigationToELM","Cmd_MapAct_SbnDeleteSbnAndSubordinates","AbortNe","Cmd_CLOG_User_Header","MultipleNeDownload","Cmd_TM_FUNCTION_admin_construct_operator","Admin","AddSwdlNes","saveSubnet","wdm_legacyManagementFunction","GetSwdl","Cmd_MapInv_SbnCreateMap","Cmd_MapAct_SbnCommitMapModify","Cmd_MapInv_ListTopLevelNtws_PHY_Test","esm_prov_customer","physconnInNet","UploadRemoteInventory","GetSwDetail","ActiveJob","NEInventory","ManageResynchro","RunPerfMon_hour","Operations","Cmd_MapWiz_ModifySubnet","AbortBkupSession","GetNeLabel","SetNeTime","InternalCmd_MapView_View_ZoomOut","NavigationToEquipments","ManageFilter","ViewBkupJobReport","DeleteSw","wdm_inventoryFunction","ManageLocalAccess","NetworkAddressRW","esm_job_reschedule","AddBkpNes","SysMonView","esm_design_template","Cmd_MapInv_PopulateSbnsInTree_PHY","addSubnet","InternalCmd_MapView_Obj_Redo","Cmd_MapInv_ShowOpcView","NetworkAddressRO","esm_design_feature","InternalCmd_MapView_View_ZoomBestFit","AlarmDebounceView","EditSwdlJobWizard","CreatePartialSwdlJob","ModifyACD","NavigationToURLs","EMLInventory","viewMap","Cmd_TM_FUNCTION_admin","esm_prov_service","Cmd_MapAct_SbnAddNode","DeleteRestoreNes","ActiveSoftware","NavigationToHostNe","AdvancedViewer","wdm_alarmManagementFunction","AllUsers","CreateSUBNE","ShowSwdlSession","EditJob","ManageSupervision","CreateNE","esm_modify_tunnel","ModifyUserLabel","Cmd_MapWiz_SbnCreateSimple","esm_modify_npa","GetNeType","Cmd_MapAct_DataSynchronize","OpenGetSwByNameWizard","InternalCmd_MapView_View_ChangeBackground","ResynchroAll","RunPerfMon_day","MulitpleNeBackup","ModifyComments","CreatePartialSwdlJobFromNeList","F_MODIFY_CUSTOMER_TEMPLATE","ManageLinkInventory","GetFtServer","esm_modify_service","ManageMib","EditRestorJobWizard","esm_deploy_networkConfig","Cmd_EventParameter","ShowJobStatus","ShowStatuses","addNode","Cmd_CLOG_User_Header_P","NavigationToAlarms","InternalCmd_MapView_Obj_Save","EditBkupJobWizard","InternalCmd_MapView_View_SwitchLayer","CreateRestoreSession","esm_job_delete","esm_prov_erp","Cmd_MapCrt_SbnCreate","Cmd_MapInv_ListObjsForCreateSbn","GetSwStatusByName","CreateSwdlSessionWizard","DirInventory","removeNode","ManageAdmin","esm_prov_lag","DeleteJob","CreateBkupSession","wdm_provisionFunction","ManageClone","modifySubnet","CommitSw","CreateME","ShowAlarms","InternalCmd_MapView_View_NormalSize","CreateRNE","InternalCmd_MapView_View_Miniature","ShowBackupNes","Construct","Cmd_Inv_SbnListPhyconnections","NERemoteConfig","ShowSWStatus","esm_modify_erp","ManageStaticLink","ForceActivateSw","GetNeTime","AlarmDebounceModify","SetMeTime","DeleteME","esm_prov_tunnel","CreateJob","GlobalAlignDownwards","esm_prov_mclag","removeSubnet","MapNodeMovement","ModifyRNEIndex","ViewSwdlJobReport","GetSwPkg","AdvancedConstruct","wdm_performanceManagementFunction","Map_FUNCTION_admin_operator","ManageNEInventory","GetNeList","OSAddressRW","ShowInformations","SwStatusSwdlJob","CreateSwdlSession","RunPerfMon_15m","cpb_operationFunction","AbortSwdlSession","Cmd_MapInv_SbnListNodes","F_DELETE_CUSTOMER_TEMPLATE","esm_prov_npa","InternalCmd_MapView_View_ZoomIn","OSAddressRO","Cmd_TM_FUNCTION_allUser","esm_modify_lag","AlignUpwards","DeleteSwdlNes","InternalCmd_MapView_Obj_Undo","ShowBkupSession","CreateDynamicLink","ViewRestoreJobReport","DisplayRI","PathBuild","ManageMonitoring","ShowRestoreNes","ManageMeTime","SysMonRead","DeleteBkpNes","DeleteSwdlSession","RemoveGeneric","ShowSwdlNes","ManageNTP","Cmd_CLOG_Admin_Header_P","ShowEqp","DeleteBkpSession","DeleteRestoreSession","Cmd_CLOG_Admin_Header","ManageNEPopulation","GetSwStatusByType","ShowIsaSWStatus","MEAddressRW","ActivateSw","esm_generate_pm_report","Cmd_Inv_LinkListConnections","Cmd_MapNav_ObjPtrToNPAview","Cmd_CLOG_Ack","MSGUI_FUNCTION_ADMIN_TOPOLOGICALVIEWS","PingNE","esm_modify_mclag","AbortJob","ManualAlignDownwards","Cmd_MapInv_ListTopLevelNtws_PHY_TOPVIEW","Cmd_MapInv_SbnListChildSbns","ShowRestoreSession","Cmd_MapAct_SbnRemoveNode","F_ADD_CUSTOMER_TEMPLATE","ManageAccess","CreateBkupSessionWizard","CreateRestoreSessionWizard","ShowJobList","MigrateME","OpenJobWizard","Cmd_TM_FUNCTION_admin_construct","SysMonAdmin","ModifyLocation","wdm_neManagementFunction","OpenGetSwByTypeWizard","ModifyME","Cmd_MapNav_OpcToMap","NavigationToELM","Cmd_MapAct_SbnDeleteSbnAndSubordinates","AbortNe","Cmd_CLOG_User_Header","MultipleNeDownload","Cmd_TM_FUNCTION_admin_construct_operator","Admin","AddSwdlNes","saveSubnet","wdm_legacyManagementFunction","GetSwdl","Cmd_MapInv_SbnCreateMap","Cmd_MapAct_SbnCommitMapModify","Cmd_MapInv_ListTopLevelNtws_PHY_Test","esm_prov_customer","physconnInNet","UploadRemoteInventory","GetSwDetail","ActiveJob","NEInventory","ManageResynchro","RunPerfMon_hour","Operations","Cmd_MapWiz_ModifySubnet","AbortBkupSession","GetNeLabel","SetNeTime","InternalCmd_MapView_View_ZoomOut","NavigationToEquipments","ManageFilter","ViewBkupJobReport","DeleteSw","wdm_inventoryFunction","ManageLocalAccess","NetworkAddressRW","esm_job_reschedule","AddBkpNes","SysMonView","esm_design_template","Cmd_MapInv_PopulateSbnsInTree_PHY","addSubnet","InternalCmd_MapView_Obj_Redo","Cmd_MapInv_ShowOpcView","NetworkAddressRO","esm_design_feature","InternalCmd_MapView_View_ZoomBestFit","AlarmDebounceView","EditSwdlJobWizard","CreatePartialSwdlJob","ModifyACD","NavigationToURLs","EMLInventory","viewMap","Cmd_TM_FUNCTION_admin","esm_prov_service","Cmd_MapAct_SbnAddNode","DeleteRestoreNes","ActiveSoftware","NavigationToHostNe","AdvancedViewer","wdm_alarmManagementFunction","AllUsers","CreateSUBNE","ShowSwdlSession","EditJob","ManageSupervision","CreateNE","esm_modify_tunnel","ModifyUserLabel","Cmd_MapWiz_SbnCreateSimple","esm_modify_npa","GetNeType","Cmd_MapAct_DataSynchronize","OpenGetSwByNameWizard","InternalCmd_MapView_View_ChangeBackground","ResynchroAll","RunPerfMon_day","MulitpleNeBackup","ModifyComments","CreatePartialSwdlJobFromNeList","F_MODIFY_CUSTOMER_TEMPLATE","ManageLinkInventory","GetFtServer","esm_modify_service","ManageMib","EditRestorJobWizard","esm_deploy_networkConfig","Cmd_EventParameter","ShowJobStatus","ShowStatuses","addNode","Cmd_CLOG_User_Header_P","NavigationToAlarms","InternalCmd_MapView_Obj_Save","EditBkupJobWizard","InternalCmd_MapView_View_SwitchLayer","CreateRestoreSession","esm_job_delete","esm_prov_erp","Cmd_MapCrt_SbnCreate","Cmd_MapInv_ListObjsForCreateSbn","GetSwStatusByName","CreateSwdlSessionWizard","DirInventory","removeNode","ManageAdmin","esm_prov_lag","DeleteJob","CreateBkupSession","wdm_provisionFunction","ManageClone","modifySubnet","CommitSw","CreateME","ShowAlarms","InternalCmd_MapView_View_NormalSize","CreateRNE","InternalCmd_MapView_View_Miniature","ShowBackupNes","Construct","Cmd_Inv_SbnListPhyconnections"],"nadString":"Voda unknown","userNadRole":"GLOBAL"}
但如果我那样做了:

x = open(r'D:\json.txt').readline()
我得到这个错误:

D:\Python\My Projects\venv\Scripts\python.exe" C:/Users/ahmedabd/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/scratch_9.py
C:/Users/ahmedabd/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/scratch_9.py:4: FutureWarning: pandas.io.json.json_normalize is deprecated, use pandas.json_normalize instead
  df = json_normalize(x)
Traceback (most recent call last):
  File "C:/Users/ahmedabd/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/scratch_9.py", line 4, in <module>
    df = json_normalize(x)
  File "D:\Python\My Projects\venv\lib\site-packages\pandas\util\_decorators.py", line 66, in wrapper
    return alternative(*args, **kwargs)
  File "D:\Python\My Projects\venv\lib\site-packages\pandas\io\json\_normalize.py", line 274, in _json_normalize
    if any([isinstance(x, dict) for x in y.values()] for y in data):
  File "D:\Python\My Projects\venv\lib\site-packages\pandas\io\json\_normalize.py", line 274, in <genexpr>
    if any([isinstance(x, dict) for x in y.values()] for y in data):
AttributeError: 'str' object has no attribute 'values'

Process finished with exit code 1
D:\Python\My Projects\venv\Scripts\Python.exe“C:/Users/ahmedabd/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratch/scratch_9.py
C:/Users/ahmedabd/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratch/scratch_9.py:4:FutureWarning:pandas.io.json.json_normalize不推荐使用,请改用pandas.json_normalize
df=json_规范化(x)
回溯(最近一次呼叫最后一次):
文件“C:/Users/ahmedabd/AppData/Roaming/JetBrains/PyCharmCE2020.1/scratches/scratch_9.py”,第4行,在
df=json_规范化(x)
文件“D:\Python\My Projects\venv\lib\site packages\pandas\util\\u decorators.py”,第66行,在包装器中
返回备选方案(*args,**kwargs)
文件“D:\Python\My Projects\venv\lib\site packages\pandas\io\json\\ u normalize.py”,第274行,在\ u json\ u normalize中
如果有([isinstance(x,dict)代表y中的x。values()]代表y中的数据):
文件“D:\Python\My Projects\venv\lib\site packages\pandas\io\json\\ u normalize.py”,第274行,在
如果有([isinstance(x,dict)代表y中的x。values()]代表y中的数据):
AttributeError:“str”对象没有属性“值”
进程已完成,退出代码为1

您可以使用
pandas
轻松做到这一点:

from pandas.io.json import json_normalize
import json
x = open("test.txt").readline()
x = json.loads(x)
df = json_normalize(x)
df = df.explode('fadList')
df.iloc[1:, 0:3] = ""
df.iloc[1:, 4:] = ""
df.to_csv("file.csv")

谢谢你的回复。我可以插入一个包含json数据的文本文件而不是{“userId”:“vendor”,“sessionId..}我想这样做。文件读取=打开(r“D:\json.txt”).readlines()x={file\u read}df=json\u规范化(x)df.to_csv(“D:\session.csv”)是的,我将json保存在文本文件中。所以,是的,我想形成一个文本文件,它适用于前4项。userId、sessionId、channelName。之后,脚本将所有数据放在一列中。我的意思是从“fadList”开始,我想因为fadList是一个列表而不是dic?
fadList
是一个列表,所以你应该有一个很长的列表。你想要什么?请共享输出,以便我可以help@AhmedAbdel-lati如果不能只有空值,一种解决方案是使用
explode
方法。