Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/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_Python 3.x_Regex - Fatal编程技术网

Python 从文本中提取文件路径

Python 从文本中提取文件路径,python,python-3.x,regex,Python,Python 3.x,Regex,我有这样的日志行 Saving stack trace for 19254 to D:\Users\tempdir\buildTmp\tmp8o258\testgroupnamelinux_stack_trace.txt Copy Dir: D:\Users\tempdir\buildTmp\tmp8fd7h5ao\build -> D:\Users\tempdir\buildTmp\tmpj7xj7ydn\build ['D:\\something\\branchname\\somewh

我有这样的日志行

Saving stack trace for 19254 to D:\Users\tempdir\buildTmp\tmp8o258\testgroupnamelinux_stack_trace.txt
Copy Dir: D:\Users\tempdir\buildTmp\tmp8fd7h5ao\build -> D:\Users\tempdir\buildTmp\tmpj7xj7ydn\build
['D:\\something\\branchname\\somewhere\\blablabla.txt'] have changes under D:\something\branchname\somewhere
Saving stack trace for 19254 to filepath
Copy Dir: filepath -> filepath
['filepath'] have changes under filepath
我想用单词
filepath
替换文件路径,以规范化我的数据。最终结果应该是这样的

Saving stack trace for 19254 to D:\Users\tempdir\buildTmp\tmp8o258\testgroupnamelinux_stack_trace.txt
Copy Dir: D:\Users\tempdir\buildTmp\tmp8fd7h5ao\build -> D:\Users\tempdir\buildTmp\tmpj7xj7ydn\build
['D:\\something\\branchname\\somewhere\\blablabla.txt'] have changes under D:\something\branchname\somewhere
Saving stack trace for 19254 to filepath
Copy Dir: filepath -> filepath
['filepath'] have changes under filepath

我正在试图找到一种方法,可以帮助我对整个数据集执行此操作。

如果您的文件将遵循特定的已知模式,您可以执行以下操作:

pattern = re.compile(r'D\:.*.txt')
# Something that starts with D: and ends with .txt, anything in between

然后您可以调用
.sub(pattern,str)

您尝试过/正在生成此输出的代码是什么?如果它不是由您打印的,那么我认为在不修改生成此输出的包的情况下,您无法执行此操作。数据可以具有不同的文件扩展名,但我可以创建所有不同文件扩展名的列表,然后对所有文件重复此过程。感谢您甚至不需要单独执行此操作,您可以创建一组扩展,例如
[.txt |.jpg |.csv |]
白色管道表示“或”。这个表达式将取代我原来答案中的
.txt
,这样更好。