英文:
Parsing data from an rss feed using python to a CSV display issue for salary field
问题
I have written a script that takes job data from an RSS feed and adds it into a CSV file, this works great! However, I came across an issue with how salary data was displaying. The below shows how the data is appended to the data list:
Append the entry data to the feed_data list
feed_data.append(
[
title,
published_date,
summary,
location,
application_email,
company,
company_website,
salary_from + " to " + salary_to,
]
)
The line salary_from + " to " + salary_to
will show a salary range, for example 35000 to 45000. However, it was actually displaying by adding 4 after the decimal point, e.g. 35000.0000 to 45000.0000.
To get around this I altered to the following:
salary_from[:-5] + " to " + salary_to[:-5]
This worked a treat and displayed as 35000 to 45000. Super! That was until I realized there are certain salaries that had a 0 value. This then caused the output to display as "to".
To try and fix this I added an if else:
salary_from[:-5] + " to " + salary_to[:-5]
if salary_from == 0
else "Unknown",
However, this would then lead to every single Salary field within the CSV file displaying as "Unknown" regardless of if it had a salary or not.
What I'm trying to achieve is just those that have a 0 value displaying as "Unknown" and those with a value displaying with the correct salary range. Thank you for any help!
英文:
I have written a script that takes job data from an RSS feed and adds it into a CSV file, this works great! However, I came across an issue with how salary data was displaying. The below shows how the data is appended to the data list:
# Append the entry data to the feed_data list
feed_data.append(
[
title,
published_date,
summary,
location,
application_email,
company,
company_website,
salary_from + " to " + salary_to,
]
)
The line salary_from + " to " + salary_to
will show a salary range, for example 35000 to 45000. However, it was actually displaying by adding 4 after the decimal point, e.g. 35000.0000 to 45000.0000.
To get around this I altered to the following:
salary_from[:-5] + " to " + salary_to[:-5]
This worked a treat and displayed as 35000 to 45000. Super! That was until I realised there are certain salaries that had a 0 value. This then caused the output to display as "to".
To try and fix this I added an if else:
salary_from[:-5] + " to " + salary_to[:-5]
if salary_from == 0
else "Unknown",
However, this would then lead to every single Salary field within the CSV file displaying as "Unknown" regardless of if it had a salary or not.
What I'm trying to achieve is just those that have a 0 value displaying as "Unknown" and those with a value displaying with the correct salary range. Thank you for any help!
答案1
得分: 2
An f-string is helpful. (Note the leading f before the string.)
if salary_from == "0":
salary_range = "Unknown"
else:
salary_range = f"{float(salary_from):.0f} to {float(salary_to):.0f}"
feed_data.append(
[
...
company_website,
salary_range,
]
)
The :.0f
formats it with 0 decimals.
英文:
An f-string is helpful. (Note the leading f before the string.)
if salary_from == "0":
salary_range = "Unknown"
else:
salary_range = f"{float(salary_from):.0f} to {float(salary_to):.0f}"
feed_data.append(
[
...
company_website,
salary_range,
]
)
The :.0f
formats it with 0 decimals.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论