Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中分析一行的文本文件_Python_File_Parsing_Text - Fatal编程技术网

在python中分析一行的文本文件

在python中分析一行的文本文件,python,file,parsing,text,Python,File,Parsing,Text,系统生成一个文本文件。它包含100多行。我想在文件里写一行 some text ** Actions Pending are: Action-1, Action-2,....Action-3 (this is another new line) some text** with open(filename) as text: for line in text: rc.logMessage(some_function_of_the_line(line)) 需要获取挂起

系统生成一个文本文件。它包含100多行。我想在文件里写一行

some text **
Actions Pending are: Action-1, Action-2,....Action-3 (this is another new line)
some text**
with open(filename) as text:
    for line in text:
         rc.logMessage(some_function_of_the_line(line))
需要获取挂起到数组中的操作

我曾经

for index in text:
    rc.logMessage(str(index))
它一次打印每个字符,而不是一行

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


提前感谢

您可以尝试以下内容:

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()])

您可以尝试以下方法:

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()])

您需要迭代文件,而不是从文件中读取字符串

some text **
Actions Pending are: Action-1, Action-2,....Action-3 (this is another new line)
some text**
with open(filename) as text:
    for line in text:
         rc.logMessage(some_function_of_the_line(line))

在文件上迭代可以得到行;对字符串进行迭代可以获得字符/字节。

您需要对文件进行迭代,而不是从文件中读取字符串

some text **
Actions Pending are: Action-1, Action-2,....Action-3 (this is another new line)
some text**
with open(filename) as text:
    for line in text:
         rc.logMessage(some_function_of_the_line(line))
在文件上迭代可以得到行;对字符串进行迭代可以得到字符/字节。

您需要str.splitlines

变成:

for index in text.splitlines():
    rc.logMessage(str(index))
你想要str.splitlines

变成:

for index in text.splitlines():
    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']
比如:

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']
Artsiom更快:,也许我的版本更可读


Artsiom更快:,也许我的版本更可读。

试试这样的东西

with file("your_file") as logfile:
   result = [line for line in logfile if line.startswith("Actions pending")]

这样,您将拥有所有操作行。

尝试类似的操作

with file("your_file") as logfile:
   result = [line for line in logfile if line.startswith("Actions pending")]

这样,您将拥有所有的操作行。

这里有一个有趣的行:

s = """some text **
Actions Pending are: Action-1, Action-2, Action-3
Actions Pending are: Action-4, Action-5, Action-6
some text**"""

[a for ln in s.splitlines() if ln.startswith("Actions Pending") for a in ln[len("Actions Pending are: "):].split(', ')]
------
['Action-1', 'Action-2', 'Action-3', 'Action-4', 'Action-5', 'Action-6']

要使用文件而不是字符串,请将s.splitlines替换为f.readlines。注意,我不会在实践中使用此代码;这只是为了好玩。

这里有一句话是为了好玩:

s = """some text **
Actions Pending are: Action-1, Action-2, Action-3
Actions Pending are: Action-4, Action-5, Action-6
some text**"""

[a for ln in s.splitlines() if ln.startswith("Actions Pending") for a in ln[len("Actions Pending are: "):].split(', ')]
------
['Action-1', 'Action-2', 'Action-3', 'Action-4', 'Action-5', 'Action-6']
要使用文件而不是字符串,请将s.splitlines替换为f.readlines。注意,我不会在实践中使用此代码;只是为了好玩