在java中的.CSV文件的开头避免使用逗号

时间:2016-04-20 04:31:33

标签: java web web-scraping screen-scraping

我正在从网站上删除数据并将其存储在CSV文件中。当数据进入CSV文件时,它会在每行的最后一个位置获得逗号。不知怎的,我设法处理它。但是,现在我在创建另一列的每一行的开头都得到了逗号。以下是我的代码。

for (Iterator<Element> it = tdElements.iterator(); it.hasNext();) {
                    if (it.hasNext()) {
                        sb.append("  \n  ");
                    }
                    for (Iterator<Element> it2 = trElement2.iterator(); it.hasNext();) {
                        Element tdElement = it.next();
                        final String content = tdElement.text();

                        if (it2.hasNext()) {
                            sb.append("   ,   ");
                            sb.append(formatData(content));
                        }

                        if (!it2.hasNext()) {
                            String content1 = content.replaceAll(",$", " ");
                            sb.append(formatData(content1));
                            break;
                        } //to remove last placed Commas.

                    }

                    System.out.println(sb.toString());
                    sb.flush();
                    sb.close();

我想要的结果例如:a,b,c,d,e
我得到的结果例如:a,b,c,d,e

2 个答案:

答案 0 :(得分:3)

如果您正在使用Java 8开发,我建议您使用StringJoiner。通过这个新课程,您不必自己构建字符串。您可以找到使用var ui = SpreadsheetApp.getUi(); var response = ui.prompt('May I know your name?', ui.ButtonSet.YES_NO); here创建CSV的示例。

我希望它有所帮助。

答案 1 :(得分:0)

StringBuffer sb = new StringBuffer(" ");
    for (Iterator<Element> it = tdElements.iterator(); it.hasNext();) {
                    if (it.hasNext()) {
                        sb.deleteCharAt(sb.length() - 1);
                        sb.append("  \n  ");
                    }
                    for (Iterator<Element> it2 = trElement2.iterator(); it.hasNext();) {
                        Element tdElement = it.next();
                        final String content = tdElement.text();

                        if (it2.hasNext()) {
                            sb.append(formatData(content));
                            sb.append(",");

                        }

                        if (!it2.hasNext()) {
                            String content1 = content.replaceAll(",$", " ");
                            sb.append(formatData(content1));
                            break;
                        } //to remove last placed Commas.

                    }

                    System.out.println(sb.toString());
                    sb.flush();
                    sb.close();


}

我尝试remove character , instance where it is trying to move to a new line instantiate stringbuffer space尝试替换我的代码 并确保 <?php $consulta = mysqli_query($conexao,"SELECT exercicios.nome_exercicio AS nome_exc , exercicios.repeticoes_exercicio AS rep_exc , exercicios.serie AS serie_exc FROM exercicios INNER JOIN usuarios ON usuarios.id_usuario = exercicios.id_usuario WHERE id_usuario = usuarios.id_usuario "); if(mysqli_num_rows($consulta) > 0) { while ($exercicio = mysqli_fetch_assoc($consulta)) { echo "<div class='table-responsive'><table class='table table-responsive'> <tr><td></td><td>Nome</td><td>Repetições</td><td>Série</td></tr> <td>'".$exercicio['nome_exc']."'</td> <td>'".$exercicio['rep_exc']."'</td> <td>'".$exercicio['serie_exc']."'</td> </table></div> "; } } else { echo "Seu instrutor ainda não cadastrou exercícios"; } ?> java.lang.IllegalStateException: Cannot call sendError() after the response has been committed at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:478) at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotWritable(DefaultHandlerExceptionResolver.java:344) at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException(DefaultHandlerExceptionResolver.java:131) at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:136) at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1120) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:944) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:279) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) 作为字符串传递。