删除空格并在txt中添加字符 [英] remove whitespace and add character in txt

查看:140
本文介绍了删除空格并在txt中添加字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MATLAB代码的问题。我想要的是你删除了所有的空白。 txt(下面的示例文件),并且可以在每行的末尾添加字符 |||||||||||||

b
$ b

示例txt:

  * | V | 0 | 0 | 0 | t | 0 | 1 | 1 | 4 | 11 | T4 | H01 ||||||| 
P | 40 | 0.01 | 10 | 1 | 1 | 0 | 40 | 1 | 1 | 1 || 1 | * || 0 | 0 | 0 ||
* | A1 | A1 | A7 | A16 | F | F | F | F | F | F | F ||||||||
* | cod。服务|区| codice | nome | tnom | tmin | tmax | pc | qc | cond | susc ||||||||
* | ciao questa rete ha 32 nodi
* ||||| kV | kV | kV | MW | MVAR | S | S ||||||||
N | I | 01 | H01N01 | H01N01 | 132 | 125.4 | 138.6 | 0 | 0 ||||||||||
N | I | 01 | H01N02 | H01N02 | 20 | 19 | 21 | 0 | 0 ||||||||||
N | I | 01 | H01N03 | H01N03 | 20 | 19 | 21 | 0.42318823 | 0.204959433 |||||||||||
N | I | 01 | H01N04 | H01N04 | 20 | 19 | 21 | 0.087176748 | 0.042221634 |||||||||||
N | I | 01 | H01N05 | H01N05 | 20 | 19 | 21 | 0.133064371 | 0.089419816 ||||||||||
N | I | 01 | H01N06 | H01N06 | 20 | 19 | 21 | 0.296231761 | 0.143471622 ||||||||||
N | I | 01 | H01N07 | H01N07 | 20 | 19 | 21 | 0.07624009 | 0.051233646 ||||||||||
N | I | 01 | H01N08 | H01N08 | 20 | 19 | 21 | 0.078459073 | 0.037999473 ||||||||||
N | I | 01 | H01N09 | H01N09 | 20 | 19 | 21 | 0.021794187 | 0.014645783 ||||||||||
N | I | 01 | H01N10 | H01N10 | 20 | 19 | 21 | 0.067710117 | 0.032793512 ||||||||||
N | I | 01 | H01N11 | H01N11 | 20 | 19 | 21 | 0.080949906 | 0.054398667 ||||||||||
N | I | 01 | H01N12 | H01N12 | 20 | 19 | 21 | 0 | 0 ||||
N | I | 01 | H01N1B | H01N1B | 20 | 19 | 21 | 0 | 0 ||||

MATLAB代码应该删除空白字符agguingere |||||| 。在输出中的txt应该是这样的:

  * | V | 0 | 0 | 0 | t | 0 | 1 | 1 | 4 | 11 | T4 | H01 ||||||||||||| 
P | 40 | 0.01 | 10 | 1 | 1 | 0 | 40 | 1 | 1 | 1 || 1 | * || 0 | 0 | 0 ||||||||
* | A1 | A1 | A7 | A16 | F | F | F | F | F | F | F ||||||||||||||
* | cod.serv | area | codice | nome | tnom | tmin | tmax | pc | qc | cond | susc ||||||||||||||
* | Ciaoquestareteha32nodi ||||||||||||
* ||||| kV | kV | kV | MW | MVAR | S | S ||||||||||||||
N | I | 01 | H01N01 | H01N01 | 132 | 125.4 | 138.6 | 0 | 0 ||||||||||||||||
N | I | 01 | H01N02 | H01N02 | 20 | 19 | 21 | 0 | 0 |||||||||||||||||
N | I | 01 | H01N03 | H01N03 | 20 | 19 | 21 | 0.42318823 | 0.204959433 ||||||||||||||||
N | I | 01 | H01N04 | H01N04 | 20 | 19 | 21 | 0.087176748 | 0.042221634 |||||||||||||||||
N | I | 01 | H01N05 | H01N05 | 20 | 19 | 21 | 0.133064371 | 0.089419816 ||||||||||||||||
N | I | 01 | H01N06 | H01N06 | 20 | 19 | 21 | 0.296231761 | 0.143471622 ||||||||||||||||
N | I | 01 | H01N07 | H01N07 | 20 | 19 | 21 | 0.07624009 | 0.051233646 ||||||||||||||||
N | I | 01 | H01N08 | H01N08 | 20 | 19 | 21 | 0.078459073 | 0.037999473 ||||||||||||||||
N | I | 01 | H01N09 | H01N09 | 20 | 19 | 21 | 0.021794187 | 0.014645783 ||||||||||||||||
N | I | 01 | H01N10 | H01N10 | 20 | 19 | 21 | 0.067710117 | 0.032793512 ||||||||||||||||
N | I | 01 | H01N11 | H01N11 | 20 | 19 | 21 | 0.080949906 | 0.054398667 ||||||||||||||||
N | I | 01 | H01N12 | H01N12 | 20 | 19 | 21 | 0 | 0 ||||||||||
N | I | 01 | H01N1B | H01N1B | 20 | 19 | 21 | 0 | 0 ||||||||||

我认为这个问题的代码是我不能错误地重写txt单元格.....'

 函数[s] = correttore()
全部清除
clc
s = textread('Filein.txt','%s','delimiter','\\\
',...
'whitespace',''); %read

s = regexprep(s,'[\''']',''); %消除空白
assignin('base','s',s)%保存工作区

%不工作

fid = fopen(.. 。)

fprintf(fid,'Fileout.txt',s)%error'单元格输入'

%不工作

%为了添加一个carapther |||||||||我认为

str ='|||||||||||||||||||||||||||||||'%但是不起作用
str = str2mat(str)%但是不要't
str = mat2cell(str(1,:))%但不工作
s(:,2)= str%但是不工作

可能是 cat 错误?任何建议?先谢谢您的帮助。

解决方案

未经测试:

  function [s] = correttore()
fidin = fopen('Filein.txt','r'); %只读,没有危险
fidout = fopen('Fileout.txt','w');
,而〜feof(fidin)
s = getl(fidin); %char数组
s = regexprep(s,'[\ s'']',''); %消除空白 - 未经测试
fprintf(fidout,'%s\\\
',[s,'|||||||||||||||||']);
end
fclose(fidin);
fclose(fidout);


I have a problem with MATLAB code. What I want is that you delete all the blanks in a. Txt (example file below) and that one could add character ||||||||||||| to the end of each line.

Example txt:

*|V|0|0|0|t|0|1|1|4|11|T4|H01|||||||
P|40|0.01|10|1|1|0|40|1|1|1||1|*||0|0|0||
*|A1|A1|A7|A16|F|F|F|F|F|F|F||||||||
*|cod.  serv|area |codice |nome|tnom|tmin|tmax|pc|qc|cond|susc||||||||
*|Ciao questa rete ha 32 nodi
*|||||kV|kV|kV|MW|MVAR|S|S||||||||
N|I|01|H01N01|H01N01|132|125.4|138.6|0|0||||||||||
N|I|01|H01N02|H01N02|20|19|21|0|0||||||||||
N|I|01|H01N03|H01N03|20|19|21|0.42318823|0.204959433||||||||||
N|I|01|H01N04|H01N04|20|19|21|0.087176748|0.042221634||||||||||
N|I|01|H01N05|H01N05|20|19|21|0.133064371|0.089419816||||||||||
N|I|01|H01N06|H01N06 |20|19|21|0.296231761|0.143471622||||||||||
N|I|01|H01N07|H01N07|20|19|21|0.07624009 |0.051233646||||||||||
N|I|01|H01N08|H01N08|20|19|21|0.078459073|0.037999473||||||||||
N|I|01|H01N09|H01N09|20|19|21|0.021794187|0.014645783||||||||||
N|I|01|H01N10|H01N10|20|19|21 |0.067710117|0.032793512||||||||||
N|I|01|H01N11|H01N11|20|19|21|0.080949906|0.054398667||||||||||
N|I|01|H01N12|H01N12 |20|19|21|0|0||||
N|I|01|H01N1B|H01N1B|20|19|21|0|0||||

The MATLAB code should remove whitespace characters agguingere ||||||. txt in the output should be like this:

*|V|0|0|0|t|0|1|1|4|11|T4|H01|||||||||||||
P|40|0.01|10|1|1|0|40|1|1|1||1|*||0|0|0||||||||
*|A1|A1|A7|A16|F|F|F|F|F|F|F||||||||||||||
*|cod.serv|area|codice|nome|tnom|tmin|tmax|pc|qc|cond|susc||||||||||||||
*|Ciaoquestareteha32nodi||||||||||||
*|||||kV|kV|kV|MW|MVAR|S|S||||||||||||||
N|I|01|H01N01|H01N01|132|125.4|138.6|0|0||||||||||||||||
N|I|01|H01N02|H01N02|20|19|21|0|0||||||||||||||||
N|I|01|H01N03|H01N03|20|19|21|0.42318823|0.204959433||||||||||||||||
N|I|01|H01N04|H01N04|20|19|21|0.087176748|0.042221634||||||||||||||||
N|I|01|H01N05|H01N05|20|19|21|0.133064371|0.089419816||||||||||||||||
N|I|01|H01N06|H01N06|20|19|21|0.296231761|0.143471622||||||||||||||||
N|I|01|H01N07|H01N07|20|19|21|0.07624009|0.051233646||||||||||||||||
N|I|01|H01N08|H01N08|20|19|21|0.078459073|0.037999473||||||||||||||||
N|I|01|H01N09|H01N09|20|19|21|0.021794187|0.014645783||||||||||||||||
N|I|01|H01N10|H01N10|20|19|21|0.067710117|0.032793512||||||||||||||||
N|I|01|H01N11|H01N11|20|19|21|0.080949906|0.054398667||||||||||||||||
N|I|01|H01N12|H01N12|20|19|21|0|0||||||||||
N|I|01|H01N1B|H01N1B|20|19|21|0|0||||||||||

The code that I thought is this the problem is that I can not rewrite the txt by mistake 'input cell .....'

function [s]=correttore()
    clear all
    clc
    s = textread('Filein.txt','%s', 'delimiter', '\n', ...
        'whitespace', '');  % Read

    s=regexprep(s,'[\s'']',''); % Eliminate white space
    assignin('base','s',s) % Save workspace

    % Don't work

    fid=fopen(...)

    fprintf(fid,'Fileout.txt',s) % error 'cell input'

    % Don't work

    %For add a carapther ||||||||| i think

    str='|||||||||||||||||' % but don't work
    str=str2mat(str) % but don't work
    str=mat2cell(str(1,:)) % but don't work
    s(:,2)=str % but don't work

Maybe is a cat error? Any suggestion? Thanks in advance for the help.

解决方案

not tested:

function [s]=correttore()
 fidin = fopen('Filein.txt','r'); % read-only, no danger
 fidout = fopen('Fileout.txt','w');
 while ~feof(fidin)
  s = getl(fidin); % char array
  s = regexprep(s,'[\s'']',''); % Eliminate white space - not tested
  fprintf(fidout,'%s\n',[s,'|||||||||||||||||']);
end
fclose(fidin);
fclose(fidout);

这篇关于删除空格并在txt中添加字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆