【发布时间】:2020-03-10 03:31:47
【问题描述】:
我需要使用python脚本从下面的文件中提取--my_dataset、--my_table和--s3_temp_path。
我的文件:
▶ cat my_datasets/my_file.yml
__global__:
role: myrole
contact: sam@user.com
__default__:
cc_policy: VERY_NEW
act_num: 16384
react_num: 16384
with_start: 1
where_to: my_file.log
class: myClass
my_arguements: >-
-Dmy.num.1=4096
-Dmy.num.2=true
-Dmy.num.3=fgcd
is_it: true
if_not: false
compure: dc1
env: test
my_compute: res-dc
config: /my/file/config
first_adhoc:
my_space: my_transfer
doodle: my_transfer.tar.gz
jar: my_transfer.jar
my_dir: "dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/dir11:my-deploy"
my_arguments: >-
m.big.class
--sdrs
--tz UTC
--env test
--my_dataset my_analytics
--my_table onboarding_client_events
--current_date 2020-09-22
--my_project my_aws_project
--s3_temp_path s3://test-wierd/
--my_key_json dir1/dir2/dir3/dir4/keys.json
--my_auth_file dir1/dir2/dir3/dir4/gcp/my_new.yml
--my_proxy example.com:9999
--write_mode write
--update_option option1 option2
first_cron:
my_space: my_transfer
doodle: my_transfer.tar.gz
jar: my_transfer.jar
my_dir: "dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10/dir11:my-deploy"
my_arguments: >-
m.big.class
--sdrs
--tz UTC
--env test
--my_dataset my_analytics
--my_table i_wish
--current_date 2020-09-22
--my_project my_aws_project
--s3_temp_path s3://test-wierd/
--my_key_json dir1/dir2/dir3/dir4/keys.json
--my_auth_file dir1/dir2/dir3/dir4/gcp/my_new.yml
--my_proxy example.com:9999
--write_mode write
--update_option option1 option2
cron_schedule: "* * 4 * *"
我在 base_path 中有很多像我上面提到的文件,从我需要获取 --my_dataset、--my_table 和 --s3_temp_path 的所有文件中。
以下是我到目前为止的时间。我能够使用 my_file.yaml 递归提取所有文件,但我无法提取上述 distinct 值。
我的脚本:
import fnmatch
import os
import re
import yaml
user_path = os.path.expanduser('~')
source_path = user_path + "/where/are/"
base_path = source_path + "/dir1/dir2/dir3/dir4/dir5/dir6/dir7/dir8/dir9/dir10"
def find(pattern, base_path):
results = []
for root, dirs, files in os.walk(base_path):
for name in files:
if fnmatch.fnmatch(name, pattern):
results.append(os.path.join(root, name))
for result in results:
stream = open(result, 'r')
dictionary = yaml.load(stream)
for key, value in dictionary.items():
res = dict((k, dictionary[k]) for k in ['my_dataset', 'my_table', 's3_temp_path' ]
if k in dictionary)
print (key + " : " + str(value))
print find('my_file.yml', base_path)
当前结果:
▶ python myWork.py
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
None
预期结果:
{"my_dataset": "my_analytics", "my_table": "i_wish", "s3_temp_path": "s3://test-wierd/"}
【问题讨论】:
-
如您所见,您需要的键不存在于字典的根级别。它们存在于
my_arguments中,而my_arguments位于另一个键本身内。是my_arguments还是my_arguements?. -
我正在尝试提取
my_arguments -
你的代码中没有这样做。
-
如果我这样做,我仍然会得到一个空字典 ``` res = dict((k, dictionary[k]) for k in ['arguments'] if k in dictionary) # print (key + " : " + str(value)) 打印分辨率 ``
标签: python python-3.x dictionary yaml python-2.x