读入时跳过空行

本文介绍了一种从CSV文件读取数据并处理其中缺失值的方法。针对含有空值的经纬度记录,提出先过滤再计算距离的解决方案,并介绍了使用SPL语言进行数据预处理的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【问题】

I have an arraylist of records that is from an input csv file. Some records have full columns but some have null. For example:

abc, 1441652452, 8.64015, 52.75034
abc, 1442279677,,
abc, 1442280570, 10.44255,148.78166

anyways, there could be more than one line of in complete records consecutively. if you look at the records, at position [2] and [3] lat and longs are being stored accordingly. what i want to do is to calculate the distance between (in the case of my example above) the third record and the fist record. (I already have the formula to calculate the distance between latlngs. Currently, this is what my code consists of:

for (int i = 0; i < oneUserRecord.size(); i++) {
            String currentRecord = oneUserRecord.get(i);
            String[] current = currentRecord.split(",");
            double currentLat = Double.parseDouble(current[3]);
            double currentLng = Double.parseDouble(current[4]);
}

What I initially planned to do was to retrieve the next record, and extract the next record's lat and long, and then find out the distance between the next and the current records' lat and longs. However, because of the incomplete records I thus won't be able to extract positions [3] and [4] simply because they don't exist in those strings. So, my question simply put is, how do I check whether positions [3] and [4] of the next record is empty, if it is empty, how do i assign the next record to be the following row of record to have a value for lat and long?

I had a logic, (continuing from the code above),

String nextRecord = "";

            if ((oneUserRecord.get(i++).split(",")[3]).isEmpty()
                    || (oneUserRecord.get(i++).split(",")[4]).isEmpty()) {
                nextRecord = oneUserRecord.get(i++);
            }
            else {
                nextRecord = oneUserRecord.get(i + 1);
            }

            String[] next = nextRecord.split(",");
            double nextLat = Double.parseDouble(next[3]);
            double nextLng = Double.parseDouble(next[4]);

but it didn't work, still got an out of bounds error. Is the a way I can improve on this, or another method for me to get the result I want?

【回答】

可以先把有空值的行过滤掉,然后就不用考虑相邻行比较的事了。可用SPL做这个事并嵌入到Java中做进一步处理:

ResultSet rs = st.executeQuery("=file("d:\\source.csv").import@c().select(#3==null || #4==null)"

不同的是,SPL返回的结果是JDBC标准的ResultSet对象,JAVA代码也可直接遍历。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值