【问题标题】:How to do text processing in java如何在java中进行文本处理
【发布时间】:2017-07-26 00:13:24
【问题描述】:

我有一个 csv 文件

 input.csv
    1,[103.85,1.28992],[103.89,1.294],[103.83,1.216]
    2,[103.5,1.292],[103.9,1.4],[103.3,1.21]
    3,[103.6,1.291],[103.6,1.39],[103.3,1.29]

从这里我需要把它转换成

{

                    "type": "LineString",
                    "coordinates": [[103.85,1.28992],[103.89,1.294],[103.83,1.216]]


                "properties": {
                    "id": "1"

                }
            },
            {

                "type": "LineString",
                "properties": {
                    "id": "2"

                },
                "coordinates": [[103.5,1.292],[103.9,1.4],[103.3,1.21]]



        },{

                "type": "LineString",
                "properties": {
                    "id": "3"

                },
                "coordinates": [[103.6,1.291],[103.6,1.39],[103.3,1.29]]



        }

我现在正在尝试在 java 中执行此操作。所以我用打开的 csv 读取了 csv 文件

try (CSVReader reader = new CSVReader(new FileReader(fileName))) {
            String[] nextLine;

            while ((nextLine = reader.readNext()) != null) {

                for (String e: nextLine) {
                   // System.out.format("%s ", e);
                System.out.println( e.split(",",1));
                }
            }

但是我在拆分线路时遇到了问题。如果您查看第一行,那么我想要 1 作为一部分,其余 [103.85,1.28992],[103.89,1.294],[103.83,1.216] 作为另一部分。这样我就可以构建字符串了

  String s="{\"type\": \"LineString\", \"coordinates\": "+s[1]+"
     \"properties\": { \"id\":"+s[0]+"} }";

感谢任何帮助

【问题讨论】:

  • @Jens 我不读取 json 文件我读取 csv 并写入 JSON 文件

标签: java regex csv text-processing opencsv


【解决方案1】:

你可以试试:

(\d+),(.*)

你不需要分裂......如果你执行它,你会得到两个 group 。第 1 组是数字,第 2 组是后面的内容 Explanation

试试这个示例:

final String regex = "(\\d+),(.*)";
final String string = "1,[103.85,1.28992],[103.89,1.294],[103.83,1.216]\n"
     + "2,[103.5,1.292],[103.9,1.4],[103.3,1.21]\n"
     + "3,[103.6,1.291],[103.6,1.39],[103.3,1.29]";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
}

【讨论】:

  • 这是要拆分spilit.("(\d+),(.*)")的参数吗?
  • 也谢谢你的解释:)
【解决方案2】:

使用 JSONSimple 创建您需要的 JSON。在我看来,最简单的 JSON 库。请参阅此example 的使用。

【讨论】:

    【解决方案3】:

    你可以自己解析这些行:

    try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
        String nextLine;
        while ((nextLine = reader.readLine()) != null) {
            int ix = nextLine.indexOf(',');
            if (ix >= 0) {
                String head = nextLine.substring(0, ix);
                String tail = nextLine.substring(ix+1);
                doSomethingWith(head, tail);
            }
        }
    }
    

    【讨论】:

    • Cannot invoke indexOf(char) on the array type String[] 为此抛出错误
    • @Josh 这是一个字符串,而不是字符串[]
    【解决方案4】:

    问题在于,要以您需要的方式获取数据,您需要拥有生成 input.csv 文件以将不同部分封装在引号中的任何内容。

    所以要么

    input.csv
       1,"[103.85,1.28992],[103.89,1.294],[103.83,1.216]"
       2,"[103.5,1.292],[103.9,1.4],[103.3,1.21]"
       3,"[103.6,1.291],[103.6,1.39],[103.3,1.29]"
    

    或者

    input.csv
       "1","[103.85,1.28992],[103.89,1.294],[103.83,1.216]"
       "2","[103.5,1.292],[103.9,1.4],[103.3,1.21]"
       "3","[103.6,1.291],[103.6,1.39],[103.3,1.29]"
    

    因为它位于行尾和行尾之间,所以有六个逗号,任何 csv 解析器都会将其解释为该行有七列而不是两列。

    【讨论】:

      猜你喜欢
      • 2011-03-02
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多