我有一个python代码,它读取一个嵌套的JSON文件,并根据下面代码中函数(如源、运算符和目标)的条件返回TRUE或FALSE,我可以得到它
现在,我想为函数(源、运算符和目标)设置一个条件,如果它满足if条件>&燃气轮机;然后,它应该重定向通过所有验证的文件名(源、运算符、目标),并将文件名移动到另一个位置
例如
def source():
if sourceName_check:
print("sourceName is vaild and starts with ES_:", sourceName_check) ---->>O/P TRUE >> pick that file and store in different directory/folder
else:
print("sourceName not starting with ES_: ", sourceName_check)
输出:如果条件为真,则>&燃气轮机;打印文件名并>&燃气轮机&燃气轮机;存储在另一个目录中
我正在读取所有JSON文件,并将其作为输入(即.JSON)传递给所有函数。将对所有文件迭代所有函数
下面是完整的代码-请帮助获取我的解决方案。谢谢 ############################################################################
#!/bin/python
import os, json
import sys
path_to_json = "C:/Users/lm989970/Downloads/flows"
def main():
for file_json in os.listdir(path_to_json):
if file_json.endswith('.json'):
print("""""######starting######""")
print(file_json)
try:
with open("{}/{}".format(path_to_json,file_json), 'r') as json_file:
json_data = json.load(json_file)
except Exception as e :
print (e)
print("JSON File is Invalid")
else:
print("JSON File is Valid")
ReadFile = json_data['flow']
get_status(ReadFile)
source(ReadFile)
operators(ReadFile)
target(ReadFile)
def get_status(ReadFile):
for index in range(0, len(ReadFile['flowVersions'])):
ReadStatus = ReadFile['flowVersions'][index]["status"]
print("status is : ", ReadStatus)
def source(ReadFile):
for index in range(0, len(ReadFile['flowVersions'])):
for i in range(0, len(ReadFile['flowVersions'][index]['topics'])):
ReadSource = ReadFile['flowVersions'][index]["topics"][i]['name']
print('sourceName is : ', ReadSource)
sourceName_check = ReadSource.startswith('ES_')
if sourceName_check:
print("sourceName is vaild and starts with ES_:", sourceName_check)
else:
print("sourceName is not starting with ES_:", sourceName_check)
def operators(ReadFile):
for index in range(0, len(ReadFile['flowVersions'])):
for i in range(0, len(ReadFile['flowVersions'][index]['operators'])):
ReadOperators = ReadFile['flowVersions'][index]["operators"][i]['name']
print('Operators are : ', ReadOperators)
operatorCheck = ReadOperators.startswith(("TRNS_", "FIL", "LKP_", "AGG_", "SSP_", "PAG_"))
if operatorCheck:
print("operator is vaild and starts with:", operatorCheck)
else:
print("operator is not starting with TRNS_:", operatorCheck)
def target(ReadFile):
for index in range(0, len(ReadFile['flowVersions'])):
for i in range(0, len(ReadFile['flowVersions'][index]['targets'])):
ReadTarget = ReadFile['flowVersions'][index]["targets"][i]['name']
print('targetName is : ', ReadTarget)
targetName_check = ReadTarget.startswith('TGT_')
if targetName_check:
print("targetName is vaild and starts with TGT_:", targetName_check)
else:
rint("targetName is not starting with TGT_:", operatorCheck)
main()
不要用python编写BASIC程序
重构代码的一部分
看看
os.path.join
您的函数
target
将不会运行相关问题 更多 >
编程相关推荐