How do I convert Python output results to JSON string using Python - python

This is my function call
if __name__ == '__main__':
a = head_tail()
b = data_info_for_analysis()
c = data_visualization_chart()
d = missing_values_duplicates()
e = mapping_yes_no()
f = one_hot_encoding()
g = outlier_identification()
out2 = removing_outliers()
h = droping, features = removing_unwanted_columns(out2)
df_telecom_test, df_telecom_train, probs, clf = random_model_predictions(droping, features)
i = logistic_model_prediction(df_telecom_train, df_telecom_test, features)
j = decision_model_prediction(df_telecom_train, df_telecom_test, features)
k = fpr_tpr_thresholds(df_telecom_test, probs, clf, features)
I am trying to save that object as a json file
filter = "JSON File (*.json)|*.json|All Files (*.*)|*.*||"
filename = a.SaveFileName("Save JSON file as", filter)
if filename:
with open(filename, 'w') as f:
json.dump(a, f)
I am getting this below error
Traceback (most recent call last):
File "/home/volumata/PycharmProjects/Churn-Analysis/sample-object-json.py", line 429, in <module>
filename = a.SaveFileName("Save JSON file as", filter)
AttributeError: 'NoneType' object has no attribute 'SaveFileName'
I have tried another method also
def head_tail():
### Head of the data
print(df_telecom.head(5))
### Tail of the data
print(df_telecom.tail(5))
code_obj = head_tail()
dis.disassemble(code_obj)
After trying this above method, getting this error
cell_names = co.co_cellvars + co.co_freevars
AttributeError: 'NoneType' object has no attribute 'co_cellvars'

For serialising a pandas.DataFrame into JSON you can use its to_json() method. There are different formatting options:
>>> df
0 1
0 a b
1 c d
>>> df.to_json()
'{"0":{"0":"a","1":"c"},"1":{"0":"b","1":"d"}}'
>>> df.to_json(orient='values')
'[["a","b"],["c","d"]]'

You question is very unclear. If you just want to convert some data from python-standard-types you can simply use json.dump:
someResults = { ... }
import json
with open("file.txt", "w") as f:
json.dump(someResults, f, indent=4)

Related

I/O why my code is always throwing and error

i stored data in a binary file.
enter code here
code 1--
import pickle as p
d = []
for i in range(3):
d1 = []
st = input("enter student name")
rl = int(input("student roll no"))
d1.append(st)
d1.append(rl)
d.extend(d1)
f = open("alex.dat", "wb")
p.dump(d,f)
f.close()
and then i printed
code 2--
import pickle as p
d = []
f = open("students.dat", "rb")
while f:
try:
d = p.load(f)
print(d)
except EOFError:
f.close()
output --
['admin', 22, 'momo', 21, 'sudhanshu', 323]
Traceback (most recent call last):
File "C:\Users\admin\AppData\Roaming\JetBrains\PyCharmCE2021.3\scratches\scratch_2.py", line 6, in
d = p.load(f)
ValueError: peek of closed file
why valueError ?
As #Maurice Mayer stated the while Condition is breaking your Code
You are writing in Code 1 everything in one file so you need just to load the file once. Checking the file-object which is already closed is breaking your Code 2
import pickle as p
d = None # Just to be sure
f = open("students.dat", "rb")
try:
d = p.load(f)
print(d)
except EOFError:
f.close()
This should work

Can't use my local json file text as intiger

import multiprocessing
import urllib.request
import json
with open("crypto.json") as f:
data = json.loads(f)
result = data
print(type(result))
resultbtc = int(result['User']['BTC'])
resultdash = int(result['User']['DASH'])
resulteth = int(result['User']['ETH'])
url = "https://min-api.cryptocompare.com/data/pricemulti?fsyms=ETH,DASH,BTC&tsyms=BTC,EUR& api_key=9a96785fb79da776270b5ffc9e989d9092bbe24d23472e107301cec5ff8a82f3"
data = urllib.request.urlopen(url)
html = data.read()
html = html.decode()
o = json.loads(html)
btcv = o['BTC']['EUR']
dashv = o['DASH']['EUR']
ethv = o['ETH']['EUR']
fresbtc = btcv * resultbtc['BTC']
fresdash = dashv * resultdash['DASH']
freseth = ethv * resulteth['ETH']
print ("Ο χρήστης",result['Name'],"εχει",fresbtc,"€ σε BITCOIN",freseth,"€ σε ETHEREUM",fresdash,"€ σε DASH")
JSON file:
[
{
"Name" : "Jonh Smith",
"BTC" : "23",
"ETH" : "345",
"DASH" : "1045"
}
]
I want to extract the values of BTC, ETH and DASH and use them as integers to be able to print their values with real time data but I get this error
Traceback (most recent call last):
File "C:\UniPapei\Εισαγωγη στην επιστημη των υπολογιστων\New folder\Εργ 4\bitcoinerg.py", line 20, in
data = json.loads(f)
File "C:\Users\Argyris\AppData\Local\Programs\Python\Python39\lib\json_init_.py", line 339, 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 TextIOWrapper
The problem is you're trying to execute loads on a file handle, not the contents of the file. Read the file's contents and pass that to json.loads()
with open("crypto.json") as f:
data = json.loads(f.read())
result = data
print(type(result))
json.loads() does not accept the file object. Instead use json.load()
with open("crypto.json") as f:
data = json.load(f)
result = data

AttributeError: int object has no add attribute. In the function of saving the prediction to a json format folder

I am writing a function to save a predictive variable to a json file. Initially, the variable was of type array.numpy16 of the form [1,0,1 ... 0, 0 1].
In the function code, I have to add an id to the data.
How do I replace the get parameter or rewrite the function
import pandas
df = pandas.DataFrame(data=predictions)
def save_output(data, path):
with open(path, mode="w") as file:
for line in sorted(data, key=lambda x: int(x.get("idx"))):
line["idx"] = int(line["idx"])
file.write(f"{json.dumps(line, ensure_ascii=False)}\n")
1 def save_output(data, path):
2 with open(path, mode="w") as file:
----> 3 for line in sorted(data, key=lambda x: int(x.get("idx"))):
4 line["idx"] = int(line["idx"])
5 file.write(f"{json.dumps(line, ensure_ascii=False)}\n")
AttributeError: 'int' object has no attribute 'get'

Uconvert json response to CSV file

i have the below json response.
{"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"}
i need to convert it to a csv file, each row has the header and below it the value. like this
userId sessionId channelName fadList NEremote ...
vendor 3d716be.. /omsevents/usession so on so on ...
i tried this solution but not working.
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()
UPDATE 1 :
Thanks to Hozafya , he provided below solution:
from pandas.io.json import json_normalize
x = open("test.txt").readline()
df = json_normalize(x)
df.to_csv("file.csv")
the script is working for the first 3 items. starting from the list, all the entire data in a one column. so i need the data to be as following for example.:
excel sheet example image
UPDATE 2 :
the solution which provided from Hozayfa solved my issue. just one more last thing, when i attach the json data directly, the script is work fine like below :
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"}
but if i did that :
x = open(r'D:\json.txt').readline()
i get this error :
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
you can easily do that using 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")

Why am i getting this error (TypeError: '_io.TextIOWrapper' object is not subscriptable) after the first iteration of my for loop?

The following code i wrote, will run one iteration with no problems. However i want it to loop through all of the values of x (which in this case there are 8). After it does the first loop through, when it goes to the second, i get an error on this line (t = f[x]['master_int'])
Traceback (most recent call last):
File "Hd5_to_KML_test.py", line 16, in <module>
t = f[x]['master_int']
TypeError: '_io.TextIOWrapper' object is not subscriptable
So it only outputs results (a .csv file and a .kml file) for BEAM0000. I was expecting it to loop through and output the two files for all 8 beams. What am I missing, why won't it loop through the other beams?
import h5py
import numpy as np
import csv
import simplekml
import argparse
parser = argparse.ArgumentParser(description='Creating a KML from an HD5 file')
parser.add_argument('HD5file', type=str)
args = parser.parse_args()
HD5file = args.HD5file
f = h5py.File(HD5file, 'r')
beamlist = []
for x in f:
t = f[x]['master_int']
for i in range(0, len(t), 1000):
time = f[x]['master_int'][i]
geolat = f[x]['geolocation']['lat_ph_bin0'][i]
geolon = f[x]['geolocation']['lon_ph_bin0'][i]
beamlist.append([time, geolat, geolon])
file = x + '.csv'
with open(file, 'w') as f:
wr = csv.writer(f)
wr.writerows(beamlist)
inputfile = csv.reader(open(file, 'r'))
kml = simplekml.Kml()
for row in inputfile:
kml.newpoint(name=row[0], coords=[(row[2], row[1])])
kml.save(file + '.kml')
When you use the context manager here:
with open(file, 'w') as f:
it reassigns to f, so when you try to access a value like f[x], it tries to call __getitem__(x) on f, which raises a TypeError
replace this block:
with open(file, 'w') as f:
wr = csv.writer(f)
wr.writerows(beamlist)
with something like:
with open(file, 'w') as fileobj:
wr = csv.writer(fileobj)
wr.writerows(beamlist)

Categories

Resources