Skip to main content
 首页 » 编程设计

python之在python中解析一行的文本文件

2025年05月04日10TianFang

系统生成一个文本文件。它包含 100 多行。我喜欢在文件中获取一行。

some text ** 
Actions Pending are: Action-1, Action-2,....Action-3 (this is another new line) 
some text** 

需要获取待处理的Actions到数组中。

我用过

for index in text: 
    rc.logMessage(str(index)) 

它一次打印每个字符而不是一行。

帮助我如何解析此文件以将操作放入数组中。

提前致谢

请您参考如下方法:

类似于:

d = """some text ** 
Actions Pending are: Action-1, Action-2, Action-3 
some text** 
""" 
res = [] 
for line in re.findall('Actions Pending are: (.+)', d): 
    res.extend([action.strip() for action in line.split(',')]) 
['Action-1', 'Action-2', 'Action-3']