-1

Extract the £ (pound) currency symbol and the amount (56) from an html file. It is printing the amount as £56 and prints the currency as Â. How can I print only 56, without the symbol? It is working fine with a $ sign.

Part of the code:

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

The output I am getting:

         Â: £56
Parmar Kamlesh
  • 151
  • 1
  • 15

2 Answers2

0

there are many ways that you can do it, you can use split, regex and one method that I did below: Hope it helps you

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

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

check here (https://ideone.com/Y053Vb)

Avinash Singh
  • 4,970
  • 8
  • 20
  • 35
  • I'm doing to get the symbol of cost i.e '£' – Avinash Singh Apr 20 '19 at 08:15
  • So? why did you create a character group? How `[\D]` (which is a group that consists of characters of the same class) is different from `\D`? https://ideone.com/yAVPAv – zerkms Apr 20 '19 at 21:01
0

Resolved: i tried to run below code in separate file in eclipse and given error about utf-8. i search the error and got answer, it is eclipse who is changing unicode style to avoid i used to run in python IDLE, i think we can change unicode in eclipse?.

Thanks to 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:
Parmar Kamlesh
  • 151
  • 1
  • 15