필터 지우기
필터 지우기

How to convert csv data with decimal csv

조회 수: 2 (최근 30일)
Mohammed Cha
Mohammed Cha 2020년 12월 18일
댓글: Mohammed Cha 2020년 12월 23일
Hello Matlab Experts,
I am converting my csv (english) version to erman version. However getting the converted csv with extra space raw. Can anyone help me?
Thank you
clear all
str = fileread('C:\Users\Cha\Desktop\TOUCH.csv');
str = strrep(str, ',', ';');
str = strrep(str, '.', ',');
f = fopen('TOUCH.csv', 'wt');
fprintf(f, '%s', str);
fclose(f);
  댓글 수: 1
Ive J
Ive J 2020년 12월 18일
Can you share your original file (TOUCH.csv) or a simulated one with the same structure?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2020년 12월 18일
Change
f = fopen('TOUCH.csv', 'wt');
to
f = fopen('TOUCH.csv', 'w');
When you do fileread(), the character vector that is read in will have inside it any carriage returns and linefeeds that are in the original text. When you fwrite() when 'wt' is active, every newline to be written will be converted to carriage return followed by newline. So what was originally CR LF in your file would get written out as CR CR LF .
If you had reason to want to force CR LF line ending, then use 'w', but
str = strrep(strrep(str, char(13), ''), char(10), char([13 10]))
This would remove all existing carriage returns and then change all newline to carriage return followed by newline.
Or alternately you could write the entire replacement series with no strrep, using
str = regexprep(str, {',', '\.', '(?<!\r)\n'}, {';', ',', '\r\n'})
This does all of the replacements, and only converts \n to \r\n if the \n was not proceeded by a \r already.
  댓글 수: 8
Walter Roberson
Walter Roberson 2020년 12월 23일
myfilename = dir('*.csv');
allfilenames = {myfilename.name};
%get rid of ones that are already _ge.csv
mask = ~cellfun(@isempty, regexp(allfilenames, '_ge\.csv$', 'once'));
allfilenames(mask) = [];
numfiles= length(allfilenames);
for k = 1:numfiles
oldname = allfilenames{K};
newname = regexprep(oldname, '\.csv', '_ge.csv');
mydata = fileread(oldname);
str = regexprep(mydata, {',','\.','(?<!\r)\n'}, {';',',','\r\n'});
f = fopen(newname, 'w');
fwrite(f, str);
fclose(f);
end
Mohammed Cha
Mohammed Cha 2020년 12월 23일
Thank you very very much Sir. It works now.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

제품


릴리스

R2015b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by