简体   繁体   English

使用 textscan 在 Octave 中读取格式错误的 CSV 文件

[英]using textscan to read badly formatted CSV file in Octave

I am trying to read a CSV file in Octave with textscan and the CSV file isn't always correctly formatted.我正在尝试使用 textscan 读取 Octave 中的textscan文件,并且 CSV 文件的格式并不总是正确。 The following mcve should illustrate the issue:以下 mcve 应说明问题:

Let's say the file is as follows:假设文件如下:

12/01/2020,12,1,2020,0,0,Russia,RU,RUS,145872260,Europe,0
11/01/2020,11,1,2020,0,0,Russia,RU,RUS,145872260,Europe,0
10/01/2020,10,1,2020,0,0,Russia,RU,RUS,145872260,Europe,0
09/01/2020,9,1,2020,0,0,Russia,RU,RUS,145872260,Europe,0
08/01/2020,8,1,2020,0,0,Russia,RU,RUS,145872260,Europe,0
07/01/2020,7,1,2020,0,0,Russia,RU,RUS,145872260,Europe,
06/01/2020,6,1,2020,0,0,Russia,RU,RUS,145872260,Europe,
05/01/2020,5,1,2020,0,0,Russia,RU,RUS,145872260,Europe,

You will notice that the final 0 is missing in the last 3 lines.您会注意到最后 3 行中缺少最后的 0。 Obviously, I can go in and manually edit the CSV files in Notepad++ or similar, but we're talking several tens of thousands of lines to go through and I just feel there must be a better solution.显然,我可以在 go 中手动编辑 CSV 文件在 Notepad++ 或类似的文件中,但我们正在谈论几万行到 go ,我只是觉得必须有一个更好的解决方案。

My code would be something like this (note that I have tried using %*f for the last element to tell Octave to skip it but that doesn't seem to work):我的代码将是这样的(请注意,我尝试使用%*f作为最后一个元素来告诉 Octave 跳过它,但这似乎不起作用):

fname = 'mcve.csv'; % the above file
fid = fopen(fname);
csv_data = textscan(fid,'%s %d %d %d %d %d %s %s %s %d %s %*f','Delimiter',',');
fclose(fid);

If you then look at csv_data , you will see that the dates are not correct (the rest of the data looks OK):如果然后查看csv_data ,您会发现日期不正确(数据的 rest 看起来不错):

>> csv_data{1}
ans =
{
  [1,1] = 12/01/2020
  [2,1] = 11/01/2020
  [3,1] = 09/01/2020
  [4,1] = 08/01/2020
  [5,1] = 07/01/2020
  [6,1] = /01/2020
  [7,1] = /01/2020
}

Any idea on how to solve this or what else to try other than the %*f I already tried?除了我已经尝试过的%*f之外,关于如何解决这个问题或尝试其他什么的任何想法?

Use csv2cell from the io package.使用csv2cell package 中的io

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM