Python Forum
Need quick help on a simple regex
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need quick help on a simple regex
#1
I need a quick help in RegEx, I need to convert some text into a particular text format.

Example text:

USD US Dollar 1.0000000000 1.0000000000
EUR Euro 0.8496903124 1.1768993778
GBP British Pound 0.7460341536 1.3404212062


There may be multiple lines.

I want that text to be converted to this format:
"First word" = "First number after the first word in the same line"

So the converted text would look like:

USD = 1.0000000000
EUR = 0.8496903124
GBP = 0.7460341536



Additionally, since I'm a complete newbie at Python and the fact that I have never used RegEx in Python (just read about it online), I would appreciate if you give me the entire sample script (i.e., including the import and etc.).

And also, I would appreciate if you could include that the numbers would be rounded off to two decimal digits.

Appreciate it.


And yes I'm playing around with trying to making a program for currency exchange. ;)
Reply
#2
welcome to the forums. unfortunately this is not how it works. we are glad to help, but you need to put effort and show us your code. we are not going to write it for you.
all that said you don't need regex for this. simple split at spaces will do. post your code in python tags, full traceback in erro r tags if you get any exceptions.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jun-10-2018, 11:31 AM)buran Wrote: welcome to the forums. unfortunately this is not how it works. we are glad to help, but you need to put effort and show us your code. we are not going to write it for you. all that said you don't need regex for this. simple split at spaces will do. post your code in python tags, full traceback in erro r tags if you get any exceptions.

aw that sucks.. but I'm up for the challenge I guess. But can you explain what you meant by "simple split at spaces"? You mean I have to split all of those into different variables and delete some variables? I kind of get what you're saying but I'm not good with loops like that so can you give me an example? Also what about when the string is a newline?

But you're saying that I should use the ".split"? Do I have to write \s or " " btw?

Also I didn't understand your last line.. "full traceback in erro r tags if you get any exceptions."

btw are you a picked volunteering moderator?
Reply
#4
>>> foo = 'EUR Euro 0.8496903124 1.1768993778'
>>> foo.split(' ')
['EUR', 'Euro', '0.8496903124', '1.1768993778']
>>> bar = foo.split(' ')
>>> bar[0]
'EUR'
>>> bar[-2]
'0.8496903124'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Jun-10-2018, 11:47 AM)buran Wrote:
>>> foo = 'EUR Euro 0.8496903124 1.1768993778' >>> foo.split(' ') ['EUR', 'Euro', '0.8496903124', '1.1768993778'] >>> bar = foo.split(' ') >>> bar[0] 'EUR' >>> bar[-2] '0.8496903124'

But how am I going to type bar[0] bar[-2] for all the lines it will take some time because there are a lot of currencies.. All the lines are one string so what about when there is a newline?
Reply
#6
here is the docs for string-formatting mini langiage - as you want to display the numbers up to 2 decimal places
https://docs.python.org/3/library/string...i-language
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
if they come as a single text:
1. split at new line ('\n'). you will get list of lines
2. loop over the result of the split and get one line at a time
3. split that line at space and get the currency and price as I show you. Print what you want inside the loop.
Now, start coding and show what you have tried...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
UPDATE:

Okay it seems that I overlooked that I had Tabs and Spaces. I used regex (which I found online) to get rid of this and now it works perfectly. Thanks!
Reply
#9
my_text = """JEP Jersey Pound    0.7460790068    1.3403406220
TMT Turkmenistani Manat 3.4999999998    0.2857142857
TVD Tuvaluan Dollar 1.3155995656    0.7601097067
IMP Isle of Man Pound   0.7460790068    1.3403406220
GGP Guernsey Pound  0.7460790068    1.3403406220
ZMW Zambian Kwacha  10.2310718223   0.0977414700"""
 
rows = my_text.split('\n')
for row in rows:
    data = [item for item in row.split(' ') if item]
    print('{} = {:.2f}'.format(data[0], float(data[-2])))
Output:
JEP = 0.75 TMT = 3.50 TVD = 1.32 IMP = 0.75 GGP = 0.75 ZMW = 10.23
(Jun-10-2018, 12:45 PM)Nwb Wrote: Ughm that's a bit confusing haha, you see I'm not that familiar with Python..
No one was proficient when start learning new language. However you need to learn using docs if you want to prosper
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A quick question teczone 4 3,091 Sep-06-2018, 03:44 PM
Last Post: snippsat
  Quick help! Ellisrb 2 2,767 May-02-2018, 11:21 AM
Last Post: ThiefOfTime
  quick way to convert in both 2 and 3 Skaperen 10 8,770 Nov-03-2016, 04:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020