TypeError: parse_pm_order_operations_dl()接受2个位置参数,但给出了3个.<



我试图解析一个嵌套的json文件,其中有一对多的关系输出。当运行我的代码时,我得到错误消息:"TypeError: parse__dl()接受2个位置参数,但给出了3个:

查看下面的代码查看当前完成的工作:

def parse_lines(self, s3obj, file_content):
"""
Parse each line ifof the file and append the parsed data to the corresponding table'
Args:
s3obj (objcet): Object that represents an object in an s3 bucket (file or dir)
file_content (str): Contents of a file.
Return:
dict: A dictionary with keys as table names and values as a listof parsed data for each table.   
"""
records = {
self.table_names[self. te_pm_order_operations_dl]: []
}
for line in file_content:
try:
record = json.loads(line)
except Exception as e:
self.logger.exception('Error processing record %s.', line)
raise e
operations_dl = self.parse_pm_order_operations_dl(s3obj, record)
if not operations_dl:
print()
else:
records[self.table_names[self.te_pm_order_operations_dl]].append(operations_dl)
return records
def parse_pm_order_operations_dl(self, record, response):
metadata = record.get('metadata')
payload = record.get('payload')
try:
plantMaintenanceOrderOperations = payload.get("plantMaintenanceOrderOperations"),
except:
return {}
if not plantMaintenanceOrderOperations:
return {}

try:
plantMaintenanceOrderOperations = json.loads(plantMaintenanceOrderOperations)
except:
return {}
result = []
for idx, item in enumerate(plantMaintenanceOrderOperations):
idx = metadata.copy()
idx.update({
'operationStandardTextKey': item["operationStandardTextKey"],
'operationActivityNumber': convert_to_int(item["operationActivityNumber"]),
'operationDescription': item["operationDescription"],
'operationStatusSystem': item["operationStatusSystem"],
'operationStatusUser': item["operationStatusUser"],
'operationConfirmationNumber': item["plantMaintenanceOrderOperationsActualData"]["operationConfirmationNumber"],
'operationActualWork': item["plantMaintenanceOrderOperationsActualData"]["operationActualWork"],
'operationEarliestScheduledStartTimestamp': item["plantMaintenanceOrderOperationsDates"]["operationEarliestScheduledStartTimestamp"],
'operationConstraintFinishTimestamp': item["plantMaintenanceOrderOperationsDates"]["operationConstraintFinishTimestamp"],
'operationLatestScheduledStartTimestamp': item["plantMaintenanceOrderOperationsDates"]["operationLatestScheduledStartTimestamp"],
'operationEarliestScheduledStartTime': item["plantMaintenanceOrderOperationsDates"]["operationEarliestScheduledStartTime"],
'operationEarliestScheduledFinishTime': item["plantMaintenanceOrderOperationsDates"]["operationEarliestScheduledFinishTime"],
'operationConstraintFinishTime': item["plantMaintenanceOrderOperationsDates"]["operationConstraintFinishTime"],
'operationConstraintStartTime': item["plantMaintenanceOrderOperationsDates"]["operationConstraintStartTime"],
'operationLatestScheduledFinishTimestamp': item["plantMaintenanceOrderOperationsDates"]["operationLatestScheduledFinishTimestamp"],
'operationEarliestScheduledStartDate': item["plantMaintenanceOrderOperationsDates"]["operationEarliestScheduledStartDate"],
'operationConstraintFinishType': item["plantMaintenanceOrderOperationsDates"]["operationConstraintFinishType"],
'operationLatestScheduledStartDate': item["plantMaintenanceOrderOperationsDates"]["operationLatestScheduledStartDate"],
'operationEarliestScheduledFinishTimestamp': item["plantMaintenanceOrderOperationsDates"]["operationEarliestScheduledFinishTimestamp"],
'operationConstraintStartDate': item["plantMaintenanceOrderOperationsDates"]["operationConstraintStartDate"],
'operationEarliestScheduledFinishDate': item["plantMaintenanceOrderOperationsDates"]["operationEarliestScheduledFinishDate"],
'operationLatestScheduledFinishTime': item["plantMaintenanceOrderOperationsDates"]["operationLatestScheduledFinishTime"],
'operationConstraintStartType': item["plantMaintenanceOrderOperationsDates"]["operationConstraintStartType"],
'operationConstraintStartTimestamp': item["plantMaintenanceOrderOperationsDates"]["operationConstraintStartTimestamp"],
'operationLatestScheduledStartTime': item["plantMaintenanceOrderOperationsDates"]["operationLatestScheduledStartTime"],
'operationLatestScheduledFinishDate': item["plantMaintenanceOrderOperationsDates"]["operationLatestScheduledFinishDate"],
'operationConstraintFinishDate': item["plantMaintenanceOrderOperationsDates"]["operationConstraintFinishDate"],
'operationWorkCenterId': item["plantMaintenanceOrderOperationsInternal"]["operationWorkCenterId"],
'operationWorkCenterDescription': item["plantMaintenanceOrderOperationsInternal"]["operationWorkCenterDescription"],
'operationWorkCenterPlantId': item["plantMaintenanceOrderOperationsInternal"]["operationWorkCenterPlantId"],
'operationControKey': item["plantMaintenanceOrderOperationsInternal"]["operationControKey"],
'operationActivityType': item["plantMaintenanceOrderOperationsInternal"]["operationActivityType"],
'operationWorkActivity': item["plantMaintenanceOrderOperationsInternal"]["operationWorkActivity"],
'operationNumberOfWorkers': convert_to_int(item["plantMaintenanceOrderOperationsInternal"]["operationNumberOfWorkers"]),
'operationDuration': item["plantMaintenanceOrderOperationsInternal"]["operationDuration"],
'operationPersonalNumber': item["plantMaintenanceOrderOperationsInternal"]["operationPersonalNumber"],      
'p_date':  get_current_date(),
'p_hour': get_current_hour()
})
result.append(idx)
return result

与json文件输出组成:

"plantMaintenanceOrderOperations": [
{
"operationStandardTextKey": "",
"plantMaintenanceOrderOperationsActualData": {
"operationActualWork": "P0Y0M0DT16H30M0S",
"operationConfirmationNumber": "0004176909"
},
"operationDescription": "Test oData Test",
"plantMaintenanceOrderOperationsDates": {
"operationEarliestScheduledStartTimestamp": "2022-11-30T06:57:08.000+01:00",
"operationConstraintFinishTimestamp": "",
"operationLatestScheduledStartTimestamp": "2022-11-30T06:57:08.000+01:00",
"operationEarliestScheduledStartTime": "07:57:08",
"operationEarliestScheduledFinishTime": "23:45:14",
"operationConstraintFinishTime": "00:00:00",
"operationConstraintStartTime": "00:00:00",
"operationLatestScheduledFinishTimestamp": "2022-12-08T22:45:14.000+01:00",
"operationEarliestScheduledStartDate": "2022-11-30",
"operationConstraintFinishType": "",
"operationLatestScheduledStartDate": "2022-11-30",
"operationEarliestScheduledFinishTimestamp": "2022-12-08T22:45:14.000+01:00",
"operationConstraintStartDate": "0000-00-00",
"operationEarliestScheduledFinishDate": "2022-12-08",
"operationLatestScheduledFinishTime": "23:45:14",
"operationConstraintStartType": "",
"operationConstraintStartTimestamp": "",
"operationLatestScheduledStartTime": "07:57:08",
"operationLatestScheduledFinishDate": "2022-12-08",
"operationConstraintFinishDate": "0000-00-00"
},
"operationStatusUser": "",
"operationActivityNumber": "0010",
"operationStatusSystem": "CNF  REL",
"plantMaintenanceOrderOperationsInternal": {
"operationWorkCenterDescription": "",
"operationWorkCenterPlantId": "A120",
"operationPersonalNumber": "00000000",
"operationWorkCenterId": "IH-MVK",
"operationDuration": "",
"operationNumberOfWorkers": 0,
"operationActivityType": "S4622",
"operationControKey": "ZIHE",
"operationWorkActivity": ""
}
}
]

如果您能帮助我添加第三个元素,我将不胜感激。

我猜一下你的问题:)

"接受2个位置参数,但给出了3个">

def foo(element1, element2): #expecting 2 args
doSomeThing()
foo(element1, element2, element3) # would result in the error, because of 3 args

你可以通过*args

来解决这个问题
def foo(*elements): 
doSomeThing()
print(elements) # list of args [element1, element2, element3]
foo(element1, element2, element3)

编辑:

其他选项:

def foo(element1, element2) #expecting 2 args
doSomeThing()
foo(element1, [element2, element3]) # passing 2 args: element1 and a list containing element2 and element3

相关内容

  • 没有找到相关文章

最新更新