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?
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File 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!