gpt4 book ai didi

python - 文字版: from Python to Matlab

转载 作者:行者123 更新时间:2023-12-01 04:51:27 24 4
gpt4 key购买 nike

我有一个 .txt 文件,如下所示:

rawdata/d-0197.bmp 1 329 210 50 51
rawdata/c-0044.bmp 1 215 287 59 48
rawdata/e-0114.bmp 1 298 244 46 45
rawdata/102.bmp 1 243 126 163 143

我需要按以下方式对其进行转换:

-在“rawdata”之前添加整个路径,即“/home/camroom/Dropbox/Internship/MyCascades/Cascade1/training/positive/”。-在“.bmp”后添加逗号

-删除第一个数字(即 1)。

-将其他四个数字放入方括号[]中。

它看起来像:

/home/camroom/Dropbox/Internship/MyCascades/Cascade1/training/positive/rawdata/d-0197.bmp, [329 210 50 51]
/home/camroom/Dropbox/Internship/MyCascades/Cascade1/training/positive/rawdata/c-0044.bmp, [215 287 59 48]
/home/camroom/Dropbox/Internship/MyCascades/Cascade1/training/positive/rawdata/e-0114.bmp, [298 244 46 45]
/home/camroom/Dropbox/Internship/MyCascades/Cascade1/training/positive/rawdata/102.bmp, [243 126 163 143]

我已经做到了,首先在简单的文本编辑器中将“rawdata/”替换为空,然后使用 python:

file=open('data.txt')
fout=open('data2.txt','w')

for line in file:
line=line.rstrip()
pieces=line.split('.bmp')
pieces2=pieces[1].split()
fout.write('/home/camroom/Dropbox/Internship/MyCascades/Cascade1/training/positive/rawdata/'+pieces[0]+'.bmp, '+'['+pieces2[1]+' '+pieces2[2]+' '+pieces2[3]+' '+pieces2[4]+']'+'\n')
fout.close()

但是这个文件将在Matlab中使用,所以最好有一个自动过程。我怎样才能在 Matlab 中做同样的事情?

谢谢

最佳答案

给你:

infid  = fopen('data.txt', 'r');
outfid = fopen('data2.txt', 'w');

dirStr = '/home/camroom/Dropbox/Internship/MyCascades/Cascade1/training/positive/';

while ~feof(infid)
inline = fgetl(infid);
outline = [dirStr, regexprep(inline,' 1 (\d* \d* \d* \d*)',', [$1]')];
fprintf(outfid, '%s\n', outline);
end

fclose(infid);
fclose(outfid);

我们所做的就是从输入文件中逐行读取代码,然后使用正则表达式对该行进行更改,然后将其写入输出文件。可能有更好的方法来应用正则表达式,但速度相当快。

关于python - 文字版: from Python to Matlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28369122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com