我有以下工作细节数据,
其中之一是
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_details”获得了所需的数据,并且应该在每一行上复制每个项目。
我该如何优雅地解决问题?
答案 0 :(得分:2)
使用zip
创建键值对,并使用dict.update
更新字典
>>> 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'}
答案 1 :(得分:1)
对于简单的人,您可以尝试这样的事情
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)