How to read in a csv file with uneven columns?

9 visualizaciones (últimos 30 días)
zepp
zepp el 17 de Oct. de 2016
Respondida: Image Analyst el 17 de Oct. de 2016
My csv file has two different types of rows:
Sep 30, 2016 15:45:09.486686950 BST 00:00:00:aa:00:df ARP 44
Sep 30, 2016 15:45:09.486688819 BST 00:00:00:aa:00:df ARP 44
Sep 30, 2016 15:45:09.486697770 BST 10.0.0.33 53830 10.0.1.35 9000 TCP 76
Sep 30, 2016 15:45:09.486711587 BST 10.0.0.33 53830 10.0.1.35 9000 TCP 76
While the date and time columns are consistent, the rest are not. In some rows, there are 4 columns containing IPv4 address and port numbers (source and destination) before the protocol column. In other rows, there is only 1 column containing a MAC address. I've tried to import the data using space delimiters but they are not very consistent as the destination IP addresses seem to fall under two or three different columns themselves because of multiple whitespaces. Is there any easy way to read this file correctly?

Respuesta aceptada

Image Analyst
Image Analyst el 17 de Oct. de 2016
You might have to read each line one at a time with fgetl() and then parse that line differently with textscan() or sscanf() depending on the length of the line you retrieved
fid = fopen('fgetl.m');
thisLine = fgetl(fid);
while ischar(thisLine)
disp(thisLine)
thisLine= fgetl(fid);
if length(thisLine) > 50 % (or whatever the short length is)
% Parse one way
else
% parse the other way
end
end
fclose(fid);

Más respuestas (0)

Categorías

Más información sobre Data Import and Analysis en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by