Python 更新dict';在没有太多项目副本的情况下,s值
我有以下工作细节数据,Python 更新dict';在没有太多项目副本的情况下,s值,python,Python,我有以下工作细节数据, 其中之一是 job_detail = { "company": "abc company recruit", "job_title": "python developer", "job_request": "20k-40k /Delhi / 3-5years / Bachelor / Full-time", "job_tags": "Big Data\nSoftware Development",
其中之一是
job_detail = {
"company": "abc company recruit",
"job_title": "python developer",
"job_request": "20k-40k /Delhi / 3-5years / Bachelor / Full-time",
"job_tags": "Big Data\nSoftware Development",
"pub_date": "10:19 published from network",
}
我尝试用以下代码提取并格式化它:
job_detail["company"] = job_detail["company"].replace("recruit", "").strip()
job_detail["job_title"] = job_detail["job_title"].strip()
job_request = [jr.strip() for jr in job_detail["job_request"].split("/")]
salary, location, experience, education, time = job_request
job_detail['salary'] = salary
job_detail['location'] = location
job_detail['experience'] = experience
job_detail["education"] = education
job_detail['time'] = time
job_detail["job_tags"] = job_detail["job_tags"].replace("\n", ",")
job_detail['pub_date'] = re.search(r"\w+", job_detail["pub_date"]).group()
跑一趟过来
In [81]: job_detail
Out[81]:
{'company': 'abc company',
'job_title': 'python developer',
'job_request': '20k-40k /Delhi / 3-5years / Bachelor / Full-time',
'job_tags': 'Big Data,Software Development',
'pub_date': '10',
'salary': '20k-40k',
'location': 'Delhi',
'experience': '3-5years',
'education': 'Bachelor',
'time': 'Full-time'}
我通过输入太多的“作业详细信息”获得了所需的数据,并且每一项都应该复制到每一行
我怎样才能优雅地解决这个问题呢?对于简单的问题,你可以试试这样的方法
job_detail = {
"company": "abc company recruit",
"job_title": "python developer",
"job_request": "20k-40k /Delhi / 3-5years / Bachelor / Full-time",
"job_tags": "Big Data\nSoftware Development",
"pub_date": "10:19 published from network",
}
salary, location, experience, education, time1 = 0,0,0,0,0
keys=["salary", "location", "experience", "education", "time1"]
for key in keys:
job_detail[key]=eval(key)
使用
zip
创建键、值对,并使用dict.update
更新dict
>>> keys=["salary", "location", "experience", "education", "time"]
>>> pairs = zip(keys, map(str.strip, job_detail["job_request"].split("/")))
>>> job_detail.update(pairs)
>>> pprint(job_detail)
{'company': 'abc company recruit',
'education': 'Bachelor',
'experience': '3-5years',
'job_request': '20k-40k /Delhi / 3-5years / Bachelor / Full-time',
'job_tags': 'Big Data\nSoftware Development',
'job_title': 'python developer',
'location': 'Delhi',
'pub_date': '10:19 published from network',
'salary': '20k-40k',
'time': 'Full-time'}
这段代码是功能性的,所以我投票将这个问题作为主题外的问题来结束可能会更好,因为-问题属于,而不是StackOverflow。