Python循环中的正则表达式

Python循环中的正则表达式,python,regex,Python,Regex,尝试清除天气状况(列表v中的索引9)并保存变量以备将来使用。难以编写适当的正则表达式来存储1个或2个单词的条件 在regexr.com上测试了我的正则表达式代码,它看起来不错,但在空闲状态下运行时不起作用 v = ['\n\n7:53 AM\n\n\n\n\n', '\n\n\n\n\n\n48 \nF\n \n\n\n\n\n\n\n', '\n\n\n\n\n\n45 \nF\n \n\n\n\n\n\n\n', '\n\n\n\n\n\n89 \n%\n \n\n\

尝试清除天气状况(列表v中的索引9)并保存变量以备将来使用。难以编写适当的正则表达式来存储1个或2个单词的条件

在regexr.com上测试了我的正则表达式代码,它看起来不错,但在空闲状态下运行时不起作用

v = ['\n\n7:53 AM\n\n\n\n\n',
 '\n\n\n\n\n\n48 \nF\n    \n\n\n\n\n\n\n',
 '\n\n\n\n\n\n45 \nF\n    \n\n\n\n\n\n\n',
 '\n\n\n\n\n\n89 \n%\n    \n\n\n\n\n\n\n',
 '\n\nSE\n\n\n\n\n',
 '\n\n\n\n\n\n5 \nmph\n    \n\n\n\n\n\n\n',
 '\n\n\n\n\n\n0 \nmph\n    \n\n\n\n\n\n\n',
 '\n\n\n\n\n\n30.11 \nin\n    \n\n\n\n\n\n\n',
 '\n\n\n\n\n\n0.0 \nin\n    \n\n\n\n\n\n\n',
 '\n\nMostly Cloudy\n\n\n\n\n']

for condition in str(v[9]):
        condition_search = re.findall('[A-Z]\w+', condition)
        if len(condition_search) > 1:
            condition = ' '
            condition = condition.join(condition_search)
        else:
            condition = str(condition_search)

print(condition)
实际结果:

'[]'
预期结果

'Mostly Cloudy'

也许,这会简单地返回:

import re
v = ['\n\n7:53 AM\n\n\n\n\n',
     '\n\n\n\n\n\n48 \nF\n    \n\n\n\n\n\n\n',
     '\n\n\n\n\n\n45 \nF\n    \n\n\n\n\n\n\n',
     '\n\n\n\n\n\n89 \n%\n    \n\n\n\n\n\n\n',
     '\n\nSE\n\n\n\n\n',
     '\n\n\n\n\n\n5 \nmph\n    \n\n\n\n\n\n\n',
     '\n\n\n\n\n\n0 \nmph\n    \n\n\n\n\n\n\n',
     '\n\n\n\n\n\n30.11 \nin\n    \n\n\n\n\n\n\n',
     '\n\n\n\n\n\n0.0 \nin\n    \n\n\n\n\n\n\n',
     '\n\nMostly Cloudy\n\n\n\n\n']

condition_search = re.findall(r'[A-Z][A-Za-z\s]+[a-z]', v[9])

print(condition_search[0])
输出
如果您希望简化/修改/探索表达式,将在的右上面板中进行解释。如果您愿意,还可以在中查看它与一些示例输入的匹配情况


正则表达式电路 可视化正则表达式:


regexp很不错,但我认为您正在寻找
.strip()

结果:

周围的空白消失了。

(在上查找文档)

由于您正在抓取一些天气数据,我假设您获得的数据在某种程度上是标准化的

查看数据,您可以知道您需要的信息在前面和后面用许多换行符和空格填充(您不需要)。要删除它们,请执行以下操作:

更简单的非正则表达式解决方案:

# This removes the leading and trailing white-space characters in each line,
# which also includes space, newline, tabs, etc,.
processed_weather_data = [line.strip() for line in v]

# Lets say you need weather condition which is at the 9th index
print(processed_weather_data[9])

if re.search(r'^[A-Z]\w*(?:\s+[A-Z]\w*)?$),v[9].strip()):condition=v[9].strip()
不需要循环。您可以对只有1个元素的列表使用
join()
,因此不需要测试。他想返回字符串,而不是列表。这太完美了,谢谢大家!我想如果我在上面使用pop,我可以快速转换为字符串。
text='\n\nMostly Cloudy\n\n\n\n\n'
print(text.strip())
Mostly Cloudy
# This removes the leading and trailing white-space characters in each line,
# which also includes space, newline, tabs, etc,.
processed_weather_data = [line.strip() for line in v]

# Lets say you need weather condition which is at the 9th index
print(processed_weather_data[9])