How to read in a csv file with uneven columns?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
zepp
am 17 Okt. 2016
Beantwortet: Image Analyst
am 17 Okt. 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?
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 17 Okt. 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);
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!