简体   繁体   English

如何使用re在python中解析列表

[英]How to parse a list in python using re

I am trying to parse a list to get the individual images returned by the bash script (stored in the list errors in py script). 我试图解析一个列表,以获取由bash脚本返回的单个图像(存储在py脚本中的列表错误中)。 How can I do this with "re" ? 如何使用“ re”做到这一点?

bash script bash脚本

#!/bin/bash
value(){
for entry in *
do
if expr "$(file -b $entry)" : 'JPEG ' >/dev/null; 
then
    echo -e "$entry"
fi  
done
}
value

python code python代码

import subprocess
errors = [subprocess.check_output(['/black.sh'])]
print errors

Output 输出量

11_37_24.jpeg
11_38_02.jpeg
11_39_56.jpeg
11_40_20.jpeg
11_40_32.jpeg
11_45_03.jpeg

The list "errors" is getting assigned a string of length 1: 列表"errors"被分配了一个长度为1的字符串:

errors = ["11_37_24.jpeg 11_38_02.jpeg 11_39_56.jpeg 11_40_20.jpeg 11_40_32.jpeg 11_45_03.jpeg"]

However, I want to get those images separately to use it in an html page. 但是,我想分别获取这些图像以在html页面中使用它。 How can I do that with "re"? 如何使用“ re”来做到这一点? Is there any other alternative? 还有其他选择吗?

I make a few assumptions (based on the example input you give): 我做一些假设(基于您提供的示例输入):

  • file names only contain digits and underscores (so I can use \\w in regex) 文件名仅包含数字和下划线(因此我可以在正则表达式中使用\\w
  • words in a file name are always concatenated by underscores (no spaces) 文件名中的单词始终由下划线连接(无空格)
  • every file is a jpeg file 每个文件都是jpeg文件

The code: 编码:

import re

errors = ["11_37_24.jpeg 11_38_02.jpeg 11_39_56.jpeg 11_40_20.jpeg 11_40_32.jpeg 11_45_03.jpeg"]

re.findall('\w+\.jpeg', errors[0])
['11_37_24.jpeg', '11_38_02.jpeg', '11_39_56.jpeg', '11_40_20.jpeg', '11_40_32.jpeg', '11_45_03.jpeg']

If my assumptions are wrong for your project, re is probably not the way to go. 如果我的假设对您的项目是错误的,那么re可能不是解决之道。

EDIT 编辑

The OP used the following code (which was given in the comments): OP使用以下代码(在注释中给出):

errors[0].split('\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM