【问题标题】:Extract the £ (pound) currency symbol and the amount (56) from an html file从 html 文件中提取 £(英镑)货币符号和金额 (56)
【发布时间】:2023-03-29 19:07:01
【问题描述】:

从 html 文件中提取 £(英镑)货币符号和金额 (56)。它将金额打印为£56,并将货币打印为Â。我怎样才能只打印 56,没有符号?使用$ 符号可以正常工作。

部分代码:

       cost= "£56"  
       currencySymbol = cost[0]
       print (currencySymbol, cost[1:])

我得到的输出:

         Â: £56

【问题讨论】:

  • 不可重现:ideone.com/z3JkXS
  • 请显示minimal reproducible example,而不仅仅是其中的一部分。
  • 如果您使用更多周围的代码上下文完成您的示例,这将有所帮助。
  • 终于用open('file.html', encoding="UTF-8")解决了
  • @greg-449 我已经在 eclipse 中安装了 pydev 插件

标签: eclipse windows-8 currency python-3.7 python-unicode


【解决方案1】:

有很多方法可以做到,你可以使用 splitregex 和我在下面做的一种方法: 希望对你有帮助

import re
cost= "£560,000"
match = re.search(r'([\D]+)([\d,]+)', cost)
output = (match.group(1), match.group(2).replace(',',''))
print (output);

output -->('£', '560000')

在这里查看 (https://ideone.com/Y053Vb)

【讨论】:

  • 我正在做的是获取成本符号,即“£”
  • 所以?为什么要创建角色组? [\D](由同一类字符组成的组)与\D 有何不同? ideone.com/yAVPAv
【解决方案2】:

已解决:我尝试在 eclipse 中的单独文件中运行以下代码并给出有关 utf-8 的错误。 我搜索错误并得到了答案,是 eclipse 正在更改 unicode 样式以避免我曾经在 python IDLE 中运行,我认为我们可以在 eclipse 中更改 unicode?。

感谢 Martijn Pieters [SyntaxError: Non-UTF-8 code starting with '\x91'

cost= "£56"  
currencySymbol = cost[0]
print (currencySymbol, cost[1:])
#resolution :when using file use encoding
#with open('index.html', encoding="UTF-8") as productFile:

【讨论】:

    猜你喜欢
    • 2019-11-04
    • 2020-12-12
    • 1970-01-01
    • 2011-11-26
    • 2020-12-20
    • 2011-03-04
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多