How to read in a csv file with uneven columns?

10 次查看(过去 30 天)
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?

采纳的回答

Image Analyst
Image Analyst 2016-10-17
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 个)

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by