[英]How do I iterate through the alphabet?
In Python, could I simply ++ a char?在 Python 中,我可以简单地 ++ 一个字符吗? What is an efficient way of doing this?
这样做的有效方法是什么?
I want to iterate through URLs that have the www.website.com/term/#, www.website.com/term/a, www.website.com/term/b, www.website.com/term/c, www.website.com/term/d ... www.website.com/term/z format.我想遍历具有 www.website.com/term/#、www.website.com/term/a、www.website.com/term/b、www.website.com/term/c、www .website.com/term/d ... www.website.com/term/z 格式。
You can use string.ascii_lowercase
which is simply a convenience string of lowercase letters,您可以使用
string.ascii_lowercase
它只是一个方便的小写字母字符串,
>>> from string import ascii_lowercase
>>> for c in ascii_lowercase:
... # append to your url
In addition to string.ascii_lowercase
you should also take a look at the ord
and chr
built-ins.除了
string.ascii_lowercase
之外,您还应该查看ord
和chr
内置string.ascii_lowercase
。 ord('a')
will give you the ascii value for 'a'
and chr(ord('a'))
will give you back the string 'a'
. ord('a')
会给你的ASCII值'a'
和chr(ord('a'))
会退给你的字符串'a'
Using these you can increment and decrement through character codes and convert back and forth easily enough.使用这些,您可以通过字符代码递增和递减,并轻松地来回转换。 ASCII table is always a good bookmark to have too.
ASCII 表也总是一个很好的书签。
I cannot comment, so I just want to give a code to @Brian answer.我无法发表评论,所以我只想给@Brian 答案提供一个代码。 https://stackoverflow.com/a/17182751/1425790
https://stackoverflow.com/a/17182751/1425790
url=www.website.com/term
my_char=ord('a') # convert char to ascii
while my_char<= ord('z'):
my_char+=1 # iterate over abc
my_url=url+chr(my_char) # convert ascii to char
do_something(my_url)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.