我正在编写一个简单的程序,该程序将一个csv文件并生成带有新列的csv。我的问题是:该程序引用旧列和所有列。 在我的代码下方
public class CSVmodifier {
public static void modify(String Path,String Escape) throws IOException {
int i=0;
String filename = new File(Path).getName().toString();
//setName(string) modify the original filename
String fileoutname = setName(filename);
File file= new File(fileoutname);
try {
FileWriter out = new FileWriter(file);
Reader reader =
Files.newBufferedReader(Paths.get(Path));
CSVReader csvReader = new CSVReader(reader);
CSVWriter csvWriter = new CSVWriter(out);
String[] nextRecord;
while ((nextRecord = csvReader.readNext()) != null) {
int dimension = nextRecord.length;
String[] newline = new String[dimension+1];
int y = 0;
//formatNumber create a string number with 9
//zero in the front-> 1 > "000000001"
newline[0]=formatNumber(i+1);
while(y<dimension) {
newline[y+1] = nextRecord[y];
y++;
}
i+=1;
csvWriter.writeNext(newline);
}
csvWriter.close();
} finally {
}
}
public static String formatNumber(int i) {
String formatted = String.format("%09d", i);
return formatted;
}
}
我的样本是:
"John","Doe","120 jefferson st.","Riverside", "NJ", "08075"
错误的输出是:
"000000001","""John""",""Doe"",""120 jefferson st."",""Riverside"", ""NJ"", ""08075"""
我无法上载文件,但是我将向您展示一个出现相同问题的示例文件(输入行):
'1231';'02512710795';'+142142';'2019/12/12';'statale';'blablabla';'iradsasad';'-123131';'+414214141';
'003';'08206810965';'+000000001492106';'2019/06/23';'Scuola statale elemetare';'Ola!'
有输出行:
'000000001';"'1231';'02512710795';'+142142';'2019/12/12';'statale';'blablabla';'iradsasad';'-123131';'+414214141'; "
'000000002';"'003';'08206810965';'+000000001492106';'2019/06/23';'Scuola statale'; "
答案 0 :(得分:0)
这对我有用,请检查并告知我。只需更改while块中的逻辑,它会将多个双引号替换为单引号。
public class CSVmodifier {
public static void modify(String Path, String Escape) throws IOException {
int i = 0;
String filename = new File(Path).getName().toString();
//setName(string) modify the original filename
String fileoutname = setName(filename);
File file = new File(fileoutname);
try {
FileWriter out = new FileWriter(file);
Reader reader
= Files.newBufferedReader(Paths.get(Path));
CSVReader csvReader = new CSVReader(reader);
CSVWriter csvWriter = new CSVWriter(out);
String[] nextRecord;
while ((nextRecord = csvReader.readNext()) != null) {
int dimension = nextRecord.length;
String[] newline = new String[dimension + 1];
int y = 0;
//formatNumber create a string number with 9
//zero in the front-> 1 > "000000001"
newline[0] = formatNumber(i + 1);
while (y < dimension) {
newline[y + 1] = nextRecord[y].replace("\"\"", "\"");
if (newline[y + 1].startsWith("\"") && newline[y + 1].endsWith("\"")) {
newline[y + 1] = newline[y + 1].substring(1, newline[y + 1].length() - 1);
}
y++;
}
i += 1;
csvWriter.writeNext(newline);
}
csvWriter.close();
} finally {
}
}
public static String formatNumber(int i) {
String formatted = String.format("%09d", i);
return formatted;
}
}
答案 1 :(得分:0)
我假设您的CSVWriter
类是com.opencsv.CSVWriter
。
您正在使用csvWriter.writeNext(String[]),这是writeNext(nextLine, true);
(JavaDoc)的快捷方式。
第二个参数在此情况下始终为applyQuotesToAll
:
如果所有值都被引用,则为真。 False仅将引号应用于包含分隔符,转义符,引号或换行符的值。
因此,您需要做的就是更改此行:
csvWriter.writeNext(newline)
对此:
csvWriter.writeNext(newline, false);