SeaDude
SeaDude

Reputation: 4405

How to extract string from python list

Feels like this should be easy, but I can't find the right keywords to search for the answer.

Given ['"https://container.blob.core.windows.net/"'] as results from a python statement...

...how do I extract only the URL and drop the ['" and "']?

Upvotes: 2

Views: 2433

Answers (4)

TaeHyoung Kwon
TaeHyoung Kwon

Reputation: 46

How about using regex??

In [35]: url_list = ['"https://container.blob.core.windows.net/"']

In [36]: url = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\), ]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', url_list[
    ...: 0])[0]

In [37]: print(url)
https://container.blob.core.windows.net/

Upvotes: 2

A l w a y s S u n n y
A l w a y s S u n n y

Reputation: 38552

How about getting first element using list[0] and remove the single quotes from it using replace() or strip() ?

 print(list[0].replace("'",""))

OR

print(list[0].strip("'")

Upvotes: 1

abc
abc

Reputation: 11949

You want the first element of the list without the first and last char

>>> l[0][1:-1]
'https://container.blob.core.windows.net/'

Upvotes: 5

Avi Thaker
Avi Thaker

Reputation: 453

try:

a = ['"https://container.blob.core.windows.net/"']
result = a[0].replace("\"","")
print(result)

Result:

'https://container.blob.core.windows.net/'

As a python string.

Upvotes: 1

Related Questions