How do I ensure that the number of delimiters on each row are constant when reading a hige csv file

22 次查看(过去 30 天)
I have a huge csv file using comma. Using the readtable function, I keep getting the error message: ''All lines of a text file must have the same number of delimiters.''.
rawdata = readtable(filename, 'Delimiter', ',');
How can I ensure that each row has the same number of delimiters?
  4 个评论
Masood Anzar
Masood Anzar 2020-4-18
How to handle if the any row of column is blank
I have a table containing data like 't=1139692479071' in each row of first column.
But some of the row are blank. I want to split the data using '=' as separator.
When I run this command ===> split(T{:,1},'=')
This error is coming : Element 2687 of the text contains 0 delimiters while the previous elements have 3.
All elements must contain the same number of delimiters.
Please advice me how to apply delimiter if the data have blank elements. I cannot remove blank rows as there is data in other columns also.
dpb
dpb 2020-4-18
You'll still have to either
  1. Insert a dummy text into blank rows like 'T=nan'
  2. Or only select the rows that have the delimiter to split
logical addressing/indexing is your friend in such cases.

请先登录,再进行评论。

回答(1 个)

Viren Gupta
Viren Gupta 2018-9-24
Making sure that each line of the input file has same number of delimiters is what the person who created this file has to do. You can only check if each line of the file has same number of delimiters. For this, you can use 'fgets()' function which gives you a string and then use 'strsplit' function based on ',' as your delimiter and then check the size of the cell array returned. If all such cells have same size, then each row has same number of delimiters else not.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by