所以我正在处理一个问题,我将文本从记事本导入java并尝试将其放入csv文件中。 我已成功设法这样做,但现在我需要从csv中的每行数组中删除信息。
所以为了帮助可视化它的这样的东西:
Name = Bob, Age = 42, FavColor = Blue Name = Clarice, Age = 20, FavColor = Red Name = John, Age = 34, FavColor = Green我想从csv的每个部分删除Name =,Age =和FavColor =以便我只有相关数据。 看起来像这样:
Bob, 42, Blue Clarice, 20, Red John, 34, Green为了进一步了解我正在做什么,我的代码到目前为止看起来像这样:
public static void main(String[] args) throws IOException{ String file_name = "C:/Users/etc...."; PrintWriter pw = new PrintWriter(new File("test.csv")); StringBuilder sb = new StringBuilder(); try { ReadFile file = new ReadFile(file_name); String[] aryLines = file.OpenFile(); sb.append("Name"); sb.append(','); sb.append("Age"); sb.append(','); sb.append("FavColor"); sb.append('\n'); int i; for(i = 0; i < aryLines.length; i++) { sb.append(aryLines[i]); sb.append("\n"); } pw.write(sb.toString()); pw.close(); System.out.println("CSV Written"); } catch (IOException e) { System.out.println( e.getMessage() ); } }阵列正确地通过记事本信息的每一行,但我只是被困在如何删除信息。 任何帮助,将不胜感激。 谢谢!
So I am dealing with a problem where I am importing text from a notepad into java and am trying to put it into a csv file. I have successfully managed to do so, but now I need to remove info from each line of the array in the csv.
So to help visualize its something like this:
Name = Bob, Age = 42, FavColor = Blue Name = Clarice, Age = 20, FavColor = Red Name = John, Age = 34, FavColor = GreenAnd I want to remove the Name =, Age = and FavColor = from each part of the csv so that I only have the relevant data. Would look like this:
Bob, 42, Blue Clarice, 20, Red John, 34, GreenFor further understanding of what I am doing my code thus far looks something like this:
public static void main(String[] args) throws IOException{ String file_name = "C:/Users/etc...."; PrintWriter pw = new PrintWriter(new File("test.csv")); StringBuilder sb = new StringBuilder(); try { ReadFile file = new ReadFile(file_name); String[] aryLines = file.OpenFile(); sb.append("Name"); sb.append(','); sb.append("Age"); sb.append(','); sb.append("FavColor"); sb.append('\n'); int i; for(i = 0; i < aryLines.length; i++) { sb.append(aryLines[i]); sb.append("\n"); } pw.write(sb.toString()); pw.close(); System.out.println("CSV Written"); } catch (IOException e) { System.out.println( e.getMessage() ); } }The array is properly going through each line of the notepad info but I just am stumped on how to get the info removed. Any help would be appreciated. Thanks!
最满意答案
尝试这个:
编辑
更改正则表达式以包含带空格的单词模式:
import java.io.*; import java.nio.charset.Charset; import java.nio.file.*; import java.util.*; public class YourClass { public static void main(String[] args) { // Path to the file being converted String infile = "C:/Users/.../test.txt"; // Some absolute path // Path to the destination output file String outfile = "C:/Users/.../test.csv"; // Some absolute path try { // Read all rows from the infile List<String> rows = Files.readAllLines(Paths.get(infile), Charset.forName("UTF-8")); // List of new rows to write to the outfile ArrayList<String> nrows = new ArrayList<String>(); // Enumerate rows for (String row : rows) { // Remove any word followed by any number of spaces and // an equals sign followed by any number of spaces String nrow = row.replaceAll("[\\w\\s]+\\s*=\\s*", ""); // Append edited row to new rows nrows.add(nrow); } // Get file descriptor Path file = Paths.get(outfile); // Write rows to outfile Files.write(file, nrows, Charset.forName("UTF-8")); // Update G/TUI System.out.println("CSV Written"); } catch (IOException e) { // You should catch specific exceptions e.printStackTrace(); } } }Try this:
EDIT
Changed the regular expression to include word patterns with spaces:
import java.io.*; import java.nio.charset.Charset; import java.nio.file.*; import java.util.*; public class YourClass { public static void main(String[] args) { // Path to the file being converted String infile = "C:/Users/.../test.txt"; // Some absolute path // Path to the destination output file String outfile = "C:/Users/.../test.csv"; // Some absolute path try { // Read all rows from the infile List<String> rows = Files.readAllLines(Paths.get(infile), Charset.forName("UTF-8")); // List of new rows to write to the outfile ArrayList<String> nrows = new ArrayList<String>(); // Enumerate rows for (String row : rows) { // Remove any word followed by any number of spaces and // an equals sign followed by any number of spaces String nrow = row.replaceAll("[\\w\\s]+\\s*=\\s*", ""); // Append edited row to new rows nrows.add(nrow); } // Get file descriptor Path file = Paths.get(outfile); // Write rows to outfile Files.write(file, nrows, Charset.forName("UTF-8")); // Update G/TUI System.out.println("CSV Written"); } catch (IOException e) { // You should catch specific exceptions e.printStackTrace(); } } }更多推荐
发布评论