从包含 python 中混合字母数字字符的文本文件中获取特定数字
Getting specific numbers from a text file containing a mix of alphanumeric characters in python
我有一个 .txt
文件,看起来像这样但更长:
Image0001_01.tif[1] <- Image0035_01.tif[1]: (410.0, -362.0) correlation (R)=0.05516124 (176 ms)
Image0001_01.tif[1] <- Image0002_01.tif[1]: (489.0, -495.0) correlation (R)=0.047715914 (287 ms)
Image0002_01.tif[1] <- Image0003_01.tif[1]: (647.0, 0.0) correlation (R)=0.8842946 (295 ms)
Image0001_01.tif[1] <- Image0036_01.tif[1]: (265.0, -363.0) correlation (R)=0.039207384 (365 ms)
Image0002_01.tif[1] <- Image0034_01.tif[1]: (626.0, -626.0) correlation (R)=0.60634625 (124 ms)
...........
我想把它变成一个逗号分隔的文件 (csv),这样我就可以查看相关性(R 值),但是 运行 因为这个文件的格式很奇怪,所以会出现问题。在 Python 中有什么方法可以做到这一点?
在python中使用re和csv解析您的文件并将其转换为csv文件:
import re
import csv
re_expression = '^(.*?) <- (.*?): \((.*?), (.*?)\) correlation \(R\)=(.*?) \((.*?) ms\)$'
with open('output.csv', 'w', newline='') as csvfile:
outfile = csv.writer(csvfile)
with open('input.txt') as f:
while True:
line = f.readline()
if not line: break
m = re.split(re_expression, line)
outfile.writerow(m[1:-1])
我有一个 .txt
文件,看起来像这样但更长:
Image0001_01.tif[1] <- Image0035_01.tif[1]: (410.0, -362.0) correlation (R)=0.05516124 (176 ms)
Image0001_01.tif[1] <- Image0002_01.tif[1]: (489.0, -495.0) correlation (R)=0.047715914 (287 ms)
Image0002_01.tif[1] <- Image0003_01.tif[1]: (647.0, 0.0) correlation (R)=0.8842946 (295 ms)
Image0001_01.tif[1] <- Image0036_01.tif[1]: (265.0, -363.0) correlation (R)=0.039207384 (365 ms)
Image0002_01.tif[1] <- Image0034_01.tif[1]: (626.0, -626.0) correlation (R)=0.60634625 (124 ms)
...........
我想把它变成一个逗号分隔的文件 (csv),这样我就可以查看相关性(R 值),但是 运行 因为这个文件的格式很奇怪,所以会出现问题。在 Python 中有什么方法可以做到这一点?
在python中使用re和csv解析您的文件并将其转换为csv文件:
import re
import csv
re_expression = '^(.*?) <- (.*?): \((.*?), (.*?)\) correlation \(R\)=(.*?) \((.*?) ms\)$'
with open('output.csv', 'w', newline='') as csvfile:
outfile = csv.writer(csvfile)
with open('input.txt') as f:
while True:
line = f.readline()
if not line: break
m = re.split(re_expression, line)
outfile.writerow(m[1:-1])