Problem to read csv file with a blank line
이전 댓글 표시
Hi folks,
I'm struggling to use date and time data of a web file, which can be saved in csv format.
I want to import the csv file starting at the six line (I dont want the information before the six line).
Every time I try to import with readtable, Matlab import this file with data and blank lines. I don't want blank lines in the midle of the data.
I just want to be able to read the date , time and the "MW" values.
Thank you very much for your help
댓글 수: 3
Cris LaPierre
2020년 9월 14일
Can you share the code you've tried so far?
Fabio Retorta
2020년 9월 14일
Your file isn't in standard format so you'll need to use delimitedTextImportOptions to specify lots of details such as
- delimiter type (;)
- number of variables
- variable names
- Variable types
- datetime formats
I've been using delimitedTextImportOptions for a while and it's still something I have to play around with when importing non-standard files. It's not easy. The Import tool can help you out but even then you have to specify many of the details above.
Check out my answer for a solution but it requires you to remove the first character of the file which is a semicolon that causes problems.
채택된 답변
추가 답변 (2개)
You should be able to use the headerlines property value pair in your call to readtable to skip the first 5 lines something like:
A = readtable('2020.csv','HeaderLines',5)
댓글 수: 2
Fabio Retorta
2020년 9월 14일
Jon
2020년 9월 14일
It seemed to work ok for me. The only glitch I noticed was that it gave me 5 columns of data rather than 4, where the last column was empty. I could easily delete that column though if it were a problem
Jeremy Hughes
2020년 9월 14일
0 개 추천
Hi,
As others have pointed out, your CSV (Comma separated value) file is actually semicolon separated. If readtable is using comma, you'll have to pass the delimiter. I am a little surprised if that isn't automatically detected, but any detection heuristic will get things wrong every now and then. (I didn't check myself)
T = readtable(filename,"Delimiter",';',"NumHeaderLines",5)
"The ideia is not to correct manualy"
If the detection of readtble doesn't give you the desired results by default, there's not anything you can do other than correct it manually (or get the wrong data).
There was a time when readtable didn't do detection--the default delimiter would be "," and NumHeaderLines = 0. Which you'd have to correct manually. (Which was the case a lot more frequently than it is now.)
I hope this helps.
J
BTW Adam Danz's soultion is better if you know this is the exact format you want to read. No detection at all.
댓글 수: 6
Adam Danz
2020년 9월 14일
Oddly enough, when I read in the file using fileread(), a bunch of null characters appear - char(0). When I remove those characters and leave in the leading semicolon, the cleaned version of the text is accepted by the file import. It's strange to me.
Cris LaPierre
2020년 9월 14일
When I look at the file with a hex editor, every other byte is a null character.
That's what it looked like with fileread. Removing those null chars did the trick. However, removing the first semicolon in the original file also allowed the importer to function. When both the null chars and the leading semicolon are present, the importer fails.
Jeremy Hughes
2020년 9월 14일
It's probably UTF-16 encoded.
Walter Roberson
2020년 9월 14일
Yes, it is UTF16-LE . Current versions (R2020a) of readtable() and textscan() will deal with that automatically.
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!