CSV 文件处理(笔试)

这篇博客是一道笔试题,要求处理CSV文件,遵循特定的字段解析规则:字段由逗号分隔,特殊字符需用引号包围。内容包括读取cvs.txt文件,将数据按规则转化:第一列转整型,第二、三列保持字符串,第四列转浮点型,第五列转日期并格式化。编程任务需在1.5小时内完成,不修改输入文件,输出到output.txt,字段以制表符分隔,字符串用单引号包围,日期格式为YYYY/MM/DD。
摘要由CSDN通过智能技术生成

题目:

# 笔试题: CSV 文件处理

给定一个 CSV 文件,其内容的展现规则如下:

- 每一行数据包含多个字段,字段间以 [,] 分割。
- 如果字段值不含有 [,] 和 ["] ,直接解析输出。
- 如果字段值内部含有逗号 [,],在在字段值两边加上双引号 ["] 将字段值括起来。
- 如果字段值内部含有双引号 ["],则字段值两边加上双引号 ["] 括起来,同时,将字段值内的一个双引号 ["] 替换为两个双引号 [""],例如: [下棋,"飞"] 在 CSV 文件中被表现为 ["下棋,""飞"""]。

## 处理要求:

读入文件 cvs.txt,根据上述 csv 文件的规则进行解析,重新格式化字段生成输出文件 output.txt


第一列转为整形(int)
第二列为字符串型
第三列为字符串型
第四列转为浮点数(float)
第五列转为日期类型(DateTime)

输出文件的字段以制表符 [TAB] 来分割字段,
字符串字段输出时用单引号[']括起来
日期字段显示成 YYYY/MM/DD 的格式

说明:
1、可以假设字段值只包含单行数据,即字段值本身不含有 [回车换行]
2、不能对文件 csv.txt 作任何修改

## 编程要求:

使用任何你熟悉的编程语言编写,时间为 1.5 小时。
 

-----------------------------------分割线-------------------------------------------------

csv文件名:csv.txt

csv 文件里的内容 

1,Jane,"下""棋,""飞""",56.2,1976-8-23
2,Kate,购物,49.6,1979-12-56
3,Jerry,"羽毛球,爬山",55.6,1980-5-26

-----------------------------------分割线-------------------------------------------------

我的答案:

import org.junit.Test;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by malin on 2019/5/16.
 */
public class ReadWrite {

    private  static  Reader reader;
    private  static BufferedReader br;

    /**
     * 文件读入方法
     */
    public static  void  readFile(String url) throws FileNotFoundException {
        File file = new File(url);
        if(!file.exists()){
            System.out.println("该路径不存在,未读取文件");
        }

         reader = new FileReader(file);
         br = new BufferedReader(reader);

        }

    public static boolean writeFile(String content, String url) throws UnsupportedEncodingException, IOException {
       File file = new File(url);
        if(!file.exists()){
            file.createNewFile();
        }

        FileOutputStream o = null;
        o = new FileOutputStream(url,true);
        o.write(content.getBytes("UTF-8"));
        o.flush();
        o.close();
        return true;
    }


    /**
     * 核心处理
     * @throws IOException
     */
    public static String core(String before) throws ParseException {

        String [] result = new String[5];
        String[] split = before.split(",");
        int length = split.length;
        result[0] = split[0];
        result[1] = "'"+split[1]+"'";
        result[3] =split[length-2];
        result[4] = dateChange(split[length-1]);
        StringBuffer sb = new StringBuffer();
        for(int i = 5 ; i<=length ; i++){
            if(i!=5){
                sb.append(",");
            }
            sb.append(split[i-3]);
        }
        result[2] = colunmChange(sb);
        return result[0]+"\t"+result[1]+"\t"+result[2]+"\t"+result[3]+"\t"+result[4]+"\n";
    }

    public static String dateChange(String date) throws ParseException {
        SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 =new SimpleDateFormat("YYYY/MM/DD");
        Date parse = sdf1.parse(date);
        String format = sdf2.format(parse);
        return format;
    }

    public static String colunmChange(StringBuffer s){
        String column3 = s.toString();
        if(column3.contains("\"")){
            column3 = column3.substring(1,column3.length()-1);
            column3 = column3.replace("\"\"","\"");
        }
        return "'"+column3+"'";
    }



    public static void main(String[] args) throws IOException {
        try{
            readFile("D:/csv.txt");
            String s = null;

            while((s = br.readLine()) != null){
                String result = core(s);
               /* list.add(result);*/
                writeFile(result,"D:/output.txt");
            }
        }catch (Exception e){
            System.out.println("异常");
            e.printStackTrace();
        }finally {
            System.out.println("执行结束,关闭输入流");
            br.close();
            reader.close();
        }


    }

}

写的仓促,并没有优化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值