5

This seems like a simple task and I'm not sure if I've accomplished it already, or if I'm chasing my tail.

values = [value.replace('-','') for value in values] ## strips out hyphen (only 1)
            print values ## outputs ['0160840020']
            parcelID = str(values) ## convert to string
            print parcelID ##outputs ['0160840020']
            url = 'Detail.aspx?RE='+ parcelID ## outputs Detail.aspx?RE=['0160840020']

As you can see I'm trying to append the number attached to the end of the URL in order to change the page via a POST parameter. My question is how do I strip the [' prefix and '] suffix? I've already tried parcelID.strip("['") with no luck. Am I doing this correctly?

David Robinson
  • 77,383
  • 16
  • 167
  • 187
smada
  • 227
  • 1
  • 7
  • 17
  • your first line is a list comprehension so you are making another list. You need to pull items out of that list as you need them. – Paul Seeb Jan 28 '13 at 18:35
  • It looks like `values` is already a list of strings. Do you really need that `str()` cast? – kojiro Jan 28 '13 at 18:37

3 Answers3

20

values is a list (of length 1), which is why it appears in brackets. If you want to get just the ID, do:

parcelID = values[0]

Instead of

parcelID = str(values)
David Robinson
  • 77,383
  • 16
  • 167
  • 187
2

Assuming you actually have a list of values when you perform this (and not just one item) this would solve you problem (it would also work for one item as you have shown)

values = [value.replace('-','') for value in values] ## strips out hyphen (only 1)

# create a list of urls from the parcelIDs
urls = ['Detail.aspx?RE='+ str(parcelID) for parcelID in values]

# use each url one at a time
for url in urls:
    # do whatever you need to do with each URL
Paul Seeb
  • 6,006
  • 3
  • 26
  • 38
0

Use the Following code to select single value from multiple value of list randomly and then convert this value to the string

location_list=["Pakistan","China","Rusia","UAE","USA","UK","Saudia Arab"]
loc=random.choice(location_list)
print(str(location_list[0]))