简体   繁体   English

Python列表到字符串转换

[英]Python List to String Conversion

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. 正如您所看到的,我正在尝试附加到URL末尾的数字,以便通过POST参数更改页面。 My question is how do I strip the [' prefix and '] suffix? 我的问题是如何删除['前缀和']后缀? I've already tried parcelID.strip("['") with no luck. 我已经尝试了parcelID.strip(“['”)没有运气。 Am I doing this correctly? 我这样做了吗?

values is a list (of length 1), which is why it appears in brackets. values是一个列表(长度为1),这就是它出现在括号中的原因。 If you want to get just the ID, do: 如果您只想获取ID,请执行以下操作:

parcelID = values[0]

Instead of 代替

parcelID = str(values)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM