我为twitter上的粉丝数量做了一个正则表达式,我必须提取它
# Create a regex for number of followers
(
(\s|-) # first separator
\d\d # first 2 digits
, # separator
\d\d\d # hundred thousands
, # separator
\d\d\d # hundreds
)
''', re.VERBOSE)
extractedFollowers = followersRegex.findall(text)
allFollowers = []
for followerCount in extractedFollowers:
allFollowers.append(followerCount[0])
但每当我运行它时,会出现:
['\n90,280,191', '\n84,239,451', '\n79,215,375', '\n75,925,596', '\n62,869,696']
如何删除\ n?
答案 0 :(得分:2)
>>> lst = ['\n90,280,191', '\n84,239,451', '\n79,215,375', '\n75,925,596', '\n62,869,696']
>>> [i.replace('\n', '') for i in lst]
# ['90,280,191', '84,239,451', '79,215,375', '75,925,596', '62,869,696']
如果您提供有关正在应用正则表达式的原始字符串的更多信息,也许我可以帮助使用正则表达式部分。
答案 1 :(得分:2)
您可以使用replace
或lstrip
。
>>>lst = ['\n90,280,191', '\n84,239,451', '\n79,215,375', '\n75,925,596', '\n62,869,696']
>>>[i.lstrip('\n') for i in lst]
['90,280,191', '84,239,451', '79,215,375', '75,925,596', '62,869,696']