Akinator
Akinator

Reputation: 3

Extracting specific info from a string in Python 3.7

Python3.7

The following is my input:

a "Random road" (1,2) (2,3) (3,4)

a is my command to add a road. Next comes the name of the road followed by its location. I need to extract the name of the road.

I wish to extract the name of the road and its coordinates and store in in separate lists. I am able to extract the integers using re but i am unable to extract the name of the road. How do I extract only the road name and store it in a separate string.

Upvotes: 0

Views: 44

Answers (1)

HazimoRa3d
HazimoRa3d

Reputation: 537

Maybe using a split method ?

test_str = "Random Road (1,2) (2,3)(3,4)" 
print(test_str.split("(")[0].strip())

'Random Road'

Edit : added simpler method if road name is between quotes

import re
test_str = """a "Random Road" (1,2) (2,3)(3,4)"""
print(re.findall('"([^"]*)"', test_str))
['Random Road']

Upvotes: 1

Related Questions