Problem to read csv file with a blank line
Afficher commentaires plus anciens
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 commentaires
Cris LaPierre
le 14 Sep 2020
Can you share the code you've tried so far?
Fabio Retorta
le 14 Sep 2020
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.
Réponse acceptée
Plus de réponses (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 commentaires
Fabio Retorta
le 14 Sep 2020
Jon
le 14 Sep 2020
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
le 14 Sep 2020
0 votes
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 commentaires
Adam Danz
le 14 Sep 2020
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
le 14 Sep 2020
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
le 14 Sep 2020
It's probably UTF-16 encoded.
Walter Roberson
le 14 Sep 2020
Yes, it is UTF16-LE . Current versions (R2020a) of readtable() and textscan() will deal with that automatically.
Catégories
En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!