Python - 如果字符串元素本身包含空格,如何用空格分割?

标签 python python-2.7

我有一个包含以下行的文件:

/home/Plugins/file1 e:222 k:dir (327/1)
/home/Plugins/file2 e:100 k:dir (326/1)

我想获取路径和元素 ID。 这很容易。

with open('output_file.txt', 'r') as output_file:
    for line in output_file:
        file_path = line.split()[0]
        eId = line.split()[1].split(":")[1]
        logging.info("file path:"+file_path)
        logging.info("eId:"+eId)

但是,问题是文件名的这个路径(第一个元素)本身可以包含空格 - 因为磁盘上的文件夹文件是用空格创建的以他们的名义(这是常见的情况)。 所以,我有这些例子:

/home/tools/AMS Provider/file3.txt e:224 k:dir (127/1)
/home/account validator e:227 k:dir (247/1)

所以路径总是第一个元素,但有时它包含空格。由于这些示例,我上面的脚本将失败。 在给定的示例中:

AMS 提供程序(子文件夹名称)

帐户验证器(路径末尾的文件名)

因为在这种情况下,路径包含空格(在子文件夹名称中,但也在路径末尾的文件名中),我如何仍然可以检索文件的路径。 怎么拆分?

注意:不幸的是,我在服务器上受限于 python 2.7。 谢谢!

最佳答案

我会使用正则表达式:

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"(.*) (e:\d*) (k:.*) \((\d{3}/\d)\)$"

test_str = ("/home/Plugins/file1 e:222 k:dir (327/1)\n"
            "/home/Plugins/file2 e:100 k:dir (326/1)\n"
            "/home/tools/AMS Provider/file3.txt e:224 k:dir (127/1)\n"
            "/home/account validator e:227 k:dir (247/1)")

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):
    
    print ("Match {matchNum} was found at {start}-{end}: {match}".
           format(matchNum = matchNum, start = match.start(),
                  end = match.end(), match = match.group()))
    
    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1
        
        print ("Group {groupNum} found at {start}-{end}: {group}".
               format(groupNum = groupNum, start = match.start(groupNum),
                      end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex
#       and u"" to prefix the test string and substitution.

输出:

Match 1 was found at 0-39: /home/Plugins/file1 e:222 k:dir (327/1)
Group 1 found at 0-19: /home/Plugins/file1
Group 2 found at 20-25: e:222
Group 3 found at 26-31: k:dir
Group 4 found at 33-38: 327/1
Match 2 was found at 40-79: /home/Plugins/file2 e:100 k:dir (326/1)
Group 1 found at 40-59: /home/Plugins/file2
Group 2 found at 60-65: e:100
Group 3 found at 66-71: k:dir
Group 4 found at 73-78: 326/1
Match 3 was found at 80-134: /home/tools/AMS Provider/file3.txt e:224 k:dir (127/1)
Group 1 found at 80-114: /home/tools/AMS Provider/file3.txt
Group 2 found at 115-120: e:224
Group 3 found at 121-126: k:dir
Group 4 found at 128-133: 127/1
Match 4 was found at 135-178: /home/account validator e:227 k:dir (247/1)
Group 1 found at 135-158: /home/account validator
Group 2 found at 159-164: e:227
Group 3 found at 165-170: k:dir
Group 4 found at 172-177: 247/1

Playground .

关于Python - 如果字符串元素本身包含空格,如何用空格分割?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72965104/

相关文章:

python - 命令完成之前子进程不会返回

python - 无法使用 Mechanize 找到表格

python - 如何在 Python 中从 3 个列表创建 3 维字典

python - 使用python打开csv文件

python - 我在 1and1 上的 python 安装停止工作(bashrc 问题)

python - 将 ndarray 转换为字符串并逐行打印

java - 在 Linux 中从 Java 或 Python(或其他技术驱动的东西)访问扫描器(但 Windows 会更好)

python - 由于带有 discord.py 的 "BAD REQUEST",消息未发送

python - 字符串到具有多个键值的字典

python - 数字,每个都存储在 1 个数组中