Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to correct the error?
#4
a non regex method using string methods....
>>> z = '346 + 324 - 368'
>>> x = z.maketrans('+-','..')
>>> x
{43: 46, 45: 46}
>>> y = z.translate(x).split('.')
>>> y
['346 ', ' 324 ', ' 368']
of course at this point its a list and each string is not separate by one single space
>>> a = list(map(str.strip, y))
>>> a
['346', '324', '368']
>>> ' '.join(a)
'346 324 368'
This would not matter of exact format as something like this
z = '346 + 324 - 368 - 368 + 368 + 368 -45 -56- 368'
would still split to this
346 324 368 368 368 368 45 56 368
Using python3.x otherwise you have to import string and convert maketrans line to.... string.maketrans('+-','..')
Recommended Tutorials:
Reply


Messages In This Thread
How to correct the error? - by Ponomarenko Pavlo - Oct-14-2016, 05:07 PM
RE: How to correct the error? - by snippsat - Oct-14-2016, 05:21 PM
RE: How to correct the error? - by Yoriz - Oct-14-2016, 05:33 PM
RE: How to correct the error? - by metulburr - Oct-14-2016, 05:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how can I correct the Bad Request error on my curl request tomtom 8 5,147 Oct-03-2021, 06:32 AM
Last Post: tomtom
  attribute error instead of correct output MaartenRo 2 2,232 Aug-28-2020, 10:22 AM
Last Post: Larz60+
  Correct py got error on another windows 7 meetinnet 8 4,075 Apr-25-2020, 04:57 PM
Last Post: meetinnet
  Getting the error like function not defined .. suggest correct code raghava 1 2,095 Feb-04-2020, 11:20 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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