系统会生成一个文本文件。它包含超过100行。我喜欢在文件中放一行。
some text **
Actions Pending are: Action-1, Action-2,....Action-3 (this is another new line)
some text**需要将操作放在pending to array中。
我用过
for index in text:
rc.logMessage(str(index))它一次打印每个字符,而不是一行。
请告诉我如何解析此文件以将操作放入数组中。
提前感谢
发布于 2011-08-04 19:30:37
类似于:
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']发布于 2011-08-04 19:28:52
您可以尝试如下所示:
pendingActions = []
textToSearch = 'Actions Pending are:'
for line in open(filename, 'r'):
line = line.strip()
if line and line.startswith(textToSearch):
pendingActions.extend([x.strip() for x in line[len(textToSearch):].split(',') if x.strip()])发布于 2011-08-04 19:29:05
您需要迭代文件,而不是从文件中读取字符串。
with open(filename) as text:
for line in text:
rc.logMessage(some_function_of_the_line(line))迭代文件得到行;迭代字符串得到字符/字节。
https://stackoverflow.com/questions/6940500
复制相似问题