什么情况你需要去写一个脚本来实现将文本文件中的数据导出到excel表呢?
前言: 如果你此时正在打算写一个脚本将文本文件中的数据导出到excel表中的指定位置去,那这篇文章你要注意看了
比如你有几十、几百个文本文件或者更多,且文本文件内全是这种下图中的内容或者其他相似内容时,一个个填入表格中去太费事费力,而且错误率还高。那么你就需要写一个脚本来实现了
那问题又来了:
- 用什么语言去写这个脚本?
- 用什么软件去写这个脚本?
博主浅浅的了解一下希望不对的话博友们不要批评我!!!
(a)c语言:由于直接使用C语言操作Excel文件相对复杂且不是标准的做法
(b)Python:适用于大量数据和复杂的文本格式,但是需要调用panda库,主要的是博主不会python!!
(c)这里我大力推荐大家使用matlab去编写脚本,因为它真的太强大了,而且很方便做起来也很方便,他的帮助文档也是强的可怕,到这里第一个问题就解决了。
万事开头难么,知道用什么去写已经不容易了。浅谈一下,博主大概快一年,没用过matlab了,上次用还是在大学! 转眼已经工作半年了,不想工作只想搞钱。算啦算啦,聊会正题。
第一步:先把设置基地址,以及目标文件地址
folderPath ='C:\Users\32616\Desktop\1'; % 请替换为您的文件夹路径
% 获取文件夹中的所有文本文件(假设文件扩展名为 .txt)
txtFiles = dir(fullfile(folderPath, '*.txt')); %打开所有文本文件
outputFilename = "C:\Users\32616\Desktop\标定参数表 - 软件测试.xlsm"; % 指定要写入的 Excel 文件、工作表名称和起始单元格
sheetName = 'Open_Front'; % 指定工作表名称
currentRow = 372; % 从D372开始
startColumn = 'D'; % 指定列S
这是自己创造的excel表
第二步:确定我们该怎么去做
因为我们每个人做的东西他不可能完全一样,我们得确认我们该怎么去做,自己先有一个做的思路,现在AI那么强大让他帮我们写代码不就完事了。。
我直接说我编程时遇到得问题吧!
-
如何获取有效数据?
a、因为我们的文件夹很大且很乱,所以我们要找到对应的文本文件比如:我要从以下文本文件中找到箭头所指的文件,我该怎么去找?
我是不是得找到他们的不同点:就是根据Pitch和Roll后面跟的数据来将他挑选出来,嗯我是找到了,但是matlab找不到啊!!!
b、matlab应该怎么去找呢?
应该先提取文件的名字,在从名字中找到Pitch()和Roll()在用一个变量来记录他们内的数据
话不多说上代码(这里我就不阐述代码的意思了,因为matlab的帮助文档比我讲的好多了):
pitchMatch = regexp(txtFiles(i).name, 'Pitch\((-?\d+\.\d+)\)', 'match', 'once');
if ~isempty(pitchMatch)
currentPitch = str2double(pitchMatch(7:end-1)); % 提取并转换 Pitch()内的数字
else
currentPitch = NaN; % 如果没有找到 Pitch,则设为 NaN
end
% 使用正则表达式提取 Roll() 括号内的数字(包括小数点)
rollMatch = regexp(txtFiles(i).name, 'Roll\((-?\d+\.\d+)\)', 'match', 'once');
if ~isempty(rollMatch)
currentRoll = str2double(rollMatch(6:end-1)); % 提取并转换 Roll 数字
else
currentRoll = NaN; % 如果没有找到 Roll,则设为 NaN
end
2、如何从文本文件中获取我们想要的内容呢?
比如我想从打开的文件夹中获取第三列数据,如下图:
代码如下:*************这个后面的内容很关键,意思为打开文本文件的第三列然后用一个data存起来,及下图红框所示,“”“”“””“”“”“”“”“”“”空格也算一列哦“”“”“”“”“”“”“”“”“”“”“”“”“”“
fileName = txtFiles(randomIndex).name;
fullFileName = fullfile(folderPath, fileName);
% Read file data here
fileID = fopen(fullFileName, 'r');
*************data = textscan(fileID, '%*f %f %*f', 'Delimiter', ' ', 'HeaderLines', 0, 'CollectOutput', true);%读文本文件中取其第三列的数据。
fclose(fileID);
3、输出据处理
因为我excel中需要显示的是横排的,因此需要将这排列数据进行转置,且我还要将他们放大1000倍
transposedDataMatrix = allThirdColumnData';%将所找到的数据进行转置
transposedDataMatrix = transposedDataMatrix * 1000;%将数据放大1000倍
第三步:将他们存入excel的指定位置
% Write to Excel here
currentStartCell = strcat(startColumn, num2str(currentRow));
writematrix(transposedDataMatrix, outputFilename, 'Sheet', sheetName, 'Range', currentStartCell);
currentRow = currentRow + 1;
到这里呢,一组数据的我们的转换就完成了。万事开头难么,我们已经成功一大半了!!!但我们不是需要转一组数据啊,如果是一组数据那我为什么不直接使用excel的转换呢?,或者我为什么不手敲呢?我们有很多很多的数据。
第四步:结果展示
这是最开始,提前设置好的excel表格:
这是运行代码后的excel表格:
上图只是我截了1/4图,下述代码还实现了Pitch( -8,-4,0,4,8,12,16)的全部数据的导入把这个发出来呢只是让大家伙看看功能是啥样的,方便大伙参考。
第五步:一堆数据的实现
话不多说,上才艺!下面代码能我本来是不想发的,因为我觉得每个人遇到的问题都不一样,挑选文本文件的具体信息都是不同的,发出来对大家没什么帮助。但是我还是发出来了毕竟做事要做全么,希望能帮大家!!!帮助博友们和博友们分享知识是我最喜欢的事情了,
folderPath ='C:\Users\32616\Desktop\SERES_F3_PDM\2_Software\5_IntegrationLayer\2_IntegrationHost\2_CANape\CalibrationMode\FL\PrintData\(49)执行器+车门\20°'; % 请替换为您的文件夹路径
% 获取文件夹中的所有文本文件(假设文件扩展名为 .txt)
txtFiles = dir(fullfile(folderPath, '*.txt')); %打开所有文本文件
outputFilename = "C:\Users\32616\Desktop\标定参数表 - 软件测试.xlsm"; % 指定要写入的 Excel 文件、工作表名称和起始单元格
sheetName = 'Close_Front'; % 指定工作表名称
currentRow = 372; % 从D372开始
startColumn = 'D'; % 指定列S
%********************************************************************************************************************************** %
%初始化矩阵来存储数据
% 预先分配内存
maxFiles = length(txtFiles);
fileIndexes = zeros(1, maxFiles);
allThirdColumnData = cell(1, maxFiles); % 假设每个文件都有数据,预先分配一个cell数组
%***************************************************************************************************************************************%
for Pitch = -16:4:16
for Roll = -12:2:12
fileIndexes = [];
for i = 1:length(txtFiles)
pitchMatch = regexp(txtFiles(i).name, 'Pitch\((-?\d+\.\d+)\)', 'match', 'once'); %匹配文件夹中找出符合格式 "Pitch(数字)" 的部分,其中的数字包括可能的负号和小数点.....
%然后把这个匹配结果作为一个字符串返回。如果文件名中没有找到匹配的部分,pitchMatch 将是空的
if ~isempty(pitchMatch)
currentPitch = str2double(pitchMatch(7:end-1)); % 提取并转换 Pitch()内的数字
else
currentPitch = NaN; % 如果没有找到 Pitch,则设为 NaN
end
% 使用正则表达式提取 Roll() 括号内的数字(包括小数点)
rollMatch = regexp(txtFiles(i).name, 'Roll\((-?\d+\.\d+)\)', 'match', 'once');
if ~isempty(rollMatch)
currentRoll = str2double(rollMatch(6:end-1)); % 提取并转换 Roll 数字
else
currentRoll = NaN; % 如果没有找到 Roll,则设为 NaN
end
if abs(currentPitch - Pitch) <= 0.3 && abs(currentRoll - Roll) <= 0.3
fileIndexes = [fileIndexes, i];%如果从文本文件“Pitch”()和“Roll”()内寻找到的值currentPitch,currentRoll,和实际的Pitch,Roll值相差在0.3以内就将他放入fileIndexes
end
end
if ~isempty(fileIndexes)
randomIndex = fileIndexes(randi(length(fileIndexes)));%从fileIndexes匹配到的值中随意挑选一个
disp(['Pitch = ', num2str(Pitch), ', Roll = ', num2str(Roll), ',文件名:', txtFiles(randomIndex).name]);%输出他对应的Pitch值,Roll值,以及文件夹名
fileName = txtFiles(randomIndex).name;
fullFileName = fullfile(folderPath, fileName);
% Read file data here
fileID = fopen(fullFileName, 'r');
%data = textscan(fileID, '%*f %f %*f', 'Delimiter', ' ', 'HeaderLines', 0, 'CollectOutput', true);%读文本文件中取其第三列的数据。
if strcmp(sheetName, 'Open_Front') || strcmp(sheetName, 'Open_Rear')
data = textscan(fileID, '%*f %f %*f', 'Delimiter', ' ', 'HeaderLines', 0, 'CollectOutput', true);%读文本文件中取其第三列的数据。
elseif strcmp(sheetName, 'Close_Front') || strcmp(sheetName, 'Close_Rear')
data = textscan(fileID, '%*f %*f %f', 'Delimiter', ' ', 'HeaderLines', 0, 'CollectOutput', true);%读文本文件中取其第五列的数据。
else
data = {}; % 其他情况,data为空
end
% 处理空的情况
if isempty(data)
warning('Data is empty for this file. Skipping...');
continue;
end
fclose(fileID);
%
allThirdColumnData = [];
for k = 1:length(data)
currentData = num2cell(data{k});
allThirdColumnData = [allThirdColumnData, currentData];
end
if all(cellfun(@(x) length(x) == length(allThirdColumnData{1}), allThirdColumnData))
allThirdColumnData = cell2mat(allThirdColumnData);
else
warning('Not all files have the same number of rows in the third column. Data is kept as a cell array.');
end
transposedDataMatrix = allThirdColumnData';%将所找到的数据进行转置
transposedDataMatrix = transposedDataMatrix * 1000;%将数据放大1000倍
% Write to Excel here
currentStartCell = strcat(startColumn, num2str(currentRow));
writematrix(transposedDataMatrix, outputFilename, 'Sheet', sheetName, 'Range', currentStartCell);
end
currentRow = currentRow + 1;
end
end