使用Python从RSS源解析数据到CSV时遇到了薪资字段的显示问题。

huangapple go评论66阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年6月29日 18:54:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580381.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定