【问题标题】:Reading data from a text file, a badly written text file从文本文件中读取数据,一个写得不好的文本文件
【发布时间】:2012-01-25 11:37:52
【问题描述】:

我正在编写一个从文本文件的行中获取数据的程序。问题是它不是最好的书面文本文件,在尝试为文件编写解析器时会有很多混乱

这里有两行这样的行,因为我都可以获得地址和纬度和经度变量,但在第二行我无法获得价格或尺寸。我不断收到的错误是-41(严重)的字符串越界异常

|12091805|,|0|,|DETAILS|,||,||,|Latitude:54.593406, Longitude:-5.934344 <b >Unit 8 Great Northern Mall Great Victoria Street Belfast Down<//b><p><p><p>Price : 150,000<p>Size: 2,411 Sq Feet  ()<p>Rent : 50,500 Per Annum<p><p>Text<p><p>|,||,||

|15961081|,|0|,|DETAILS|,||,||,|<p>Latitude:54.593406, Longitude:-5.934344   <b>3-5 Market Street Lurgan BT66</b> </p>  <p> </p>  <p> </p>  <p>   Price : &pound;250,000 </p>  <p>   Size: 0.173 acres (0.07ha) </p>  <p> </p>  <p>   Text </p>  <p> </p>  <p>  Text </p>  <p> </p>  <p>   Text </p>  <p> </p>  <p> </p>|,||,||

它更长,但我现在只是为了说文字而更改了段落。

不,我不能重写文本文件。任何指针将不胜感激

if (s.contains("Price"))
{
    int pstart = 0;
    int pend = 0;

    if (s.contains("<p>Size"))
    {

        //if has pound symbol
        if (s.contains("&pound;"))
        {
            String[] str = s.split("&pound;");
            StringBuilder bs = new StringBuilder();
            for (String st : str)
            {
                bs.append(st);
            }

            pstart = bs.indexOf("Price") + 8;
            pend = bs.indexOf("</p>") - 1;
        }
        else
        {
            pstart = s.indexOf("Price") + 8;
            pend = s.indexOf("<p>Size");
        }

        String sp = s.substring(pstart, pend);

        String[] spl = sp.split(",");
        StringBuilder build = new StringBuilder();
        for (String st : spl)
        {
            build.append(st);
            f = build.toString();
        }
        in = Integer.parseInt(f);
        p.setPrice(in);
    }
    else
    {
        if (s.contains("&pound;"))
        {
            String[] str = s.split("&pound;");
            StringBuilder bs = new StringBuilder();
            for (String st : str)
            {
                bs.append(st);
            }

            pstart = bs.indexOf("Price : ");
            pend = bs.indexOf("</p>") - 1;
        }
        else
        {
            pstart = s.indexOf("Price") + 8;
            pend = s.indexOf("<p>Size");
        }

        String sp = s.substring(pstart, pend);

        String[] spl = sp.split(",");
        StringBuilder build = new StringBuilder();
        for (String st : spl)
        {
            build.append(st);
            f = build.toString();
        }
        in = Integer.parseInt(f);
        p.setPrice(in);
    }
}

// if has size property
if (s.contains("Size"))
{
    //if in acres
    if (s.contains("acres"))
    {
        int sstart = s.indexOf("Size:") + 6;
        int send = s.indexOf("acres") - 1;

        String sp = s.substring(sstart, send);
        double d = Double.parseDouble(sp);

        p.setSized(d);

    }

    if (s.contains("()"))
    {
        int sstart = s.indexOf("Size:") + 6;

        int send = s.indexOf("Sq") - 2;

        String sp = s.substring(sstart, send);

        if (sp.contains("-") && sp.contains(","))
        {
            String[] spl = sp.split("-|,");

            StringBuilder str = new StringBuilder();
            str.append(spl[0] + spl[1]);

            StringBuilder str2 = new StringBuilder(0);
            str2.append(spl[2] + spl[3]);

            String s1 = str.toString();
            int i = Integer.parseInt(s1);
            p.setSize(i);

            String s2 = str2.toString();
            i = Integer.parseInt(s2);
            p.setSize2(i);
        }

        if (sp.contains("-"))
        {
            String[] spl = sp.split("-");

            int one = Integer.parseInt(spl[0]);

            p.setSize(one);

            int two = Integer.parseInt(spl[1]);

            p.setSize2(two);

        }
        else if (!(sp.contains("-")))
        {
            if (sp.contains(","))
            {
                String[] spl = sp.split(",");
                StringBuilder build = new StringBuilder();
                for (String st : spl)
                {
                    build.append(st);
                    f = build.toString();
                }
                in = Integer.parseInt(f);
                p.setSize(in);
            }
            else
            {
                p.setSize(Integer.parseInt(sp));
            }

        }

    }

}
v.add(p);
p = new Property();

【问题讨论】:

  • 如果您向我们展示文件的外观示例会有所帮助
  • 是文本文件还是html文件?
  • 抱歉,添加到行数据中
  • 文件有多大?如果它不是很大,只需将标签

    ... 替换为普通拆分器,无论是昏迷还是管道或其他任何东西。使用 sed 这样做。你会节省很多时间而不是浪费时间。

  • ...... 用 sed 或 replace() 用逗号替换标签,然后使用你的 .split() 就可以了。

标签: java file parsing variables text


【解决方案1】:

我会使用正则表达式,以下应该为您指明正确的方向:

Pattern pricePattern = Pattern.compile("Price\\s*:\\s*(&pound;)?([0-9,.]+)"); 
Pattern sqFeetPattern = Pattern.compile("Size\\s*:\\s*([0-9,.]+)\\s*Sq"); 
Pattern acresPattern = Pattern.compile("Size\\s*:\\s*([0-9,.]+)\\s*acres\\s*\\(([0-9,.]+)ha\\)"); 

NumberFormat nf = NumberFormat.getNumberInstance();
nf.setGroupingUsed(true);

BufferedReader r = new BufferedReader(inputFileReader);
String line;
while ((line = r.readLine()) != null) {
    Matcher m = pricePattern.matcher(line);
    if (m.find()) {
        int price = nf.parse(m.group(2)).intValue();
        System.out.println("Price: " + price);
    }
    m = sqFeetPattern.matcher(line);
    if (m.find()) {
        int sqFeet = nf.parse(m.group(1)).intValue();
        System.out.println("Sq Feet: " + sqFeet);
    }
    m = acresPattern.matcher(line);
    if (m.find()) {
        float acres = nf.parse(m.group(1)).floatValue();
        float ha = nf.parse(m.group(2)).floatValue();
        System.out.println("Acres: " + acres + " ha: " + ha);
    }
}

注意inputFileReader 将被定义为 FileReader 或类似名称以获取您的文件。

【讨论】:

    【解决方案2】:

    我会采取的方法是。

    1. 阅读文本行
    2. 解码文本行 - 看起来像 HTML 标记,因此将转义字符(例如&amp;pound;)转换为等效的文本字符并过滤掉 HTML 标记(&lt;p&gt; 等)
    3. 使用正则表达式对清理后的数据执行数据提取
    4. 过程数据
    5. 下一行或结束。

    对于第 2 步,我的想法是这样的。因此,在将字符串拆分为字段分隔符 (|)

    之前,您先从字符串中剥离所有 html 标记

    Remove HTML tags from a String

    【讨论】:

    • 这就是我一直在做的事情,但是如果您查看这些行,您会发现一行中的价格数字后面跟着一个与另一行不同的元素。
    • 替换价格:&磅;到价格:无处不在
    • 这就是问题所在,我根本无法更改文本文件,我以后必须使用的文本文件完全相同,但在外部数据库中,所以我现在无法更改,得到它工作并称之为完成,不是那样工作的。
    • 我不是要更改文本文件。这一切都可以在您读入每个字符串时完成。有关步骤 2 的更多说明,请参阅更新的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2016-08-14
    • 2015-11-08
    • 2019-05-15
    相关资源
    最近更新 更多