当我尝试读取csv时,我收到此错误消息:
Exception in thread "main" java.lang.IllegalStateException: No header mapping was specified, the record values can't be accessed by name
at org.apache.commons.csv.CSVRecord.get(CSVRecord.java:99)
at mockdata.MockData.main(MockData.java:33)
Run Code Online (Sandbox Code Playgroud)
Java结果:1
我正在使用Apache Commons CSV库1.1.尝试使用Google搜索错误消息,我唯一得到的是像grepcode这样的网站上的代码列表.
这是我的代码:
package mockdata;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
public class MockData
{
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException
{
Reader in = new InputStreamReader(MockData.class.getClassLoader()
.getResourceAsStream("MOCK_DATA.csv"), "UTF-8");
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
for (CSVRecord record …Run Code Online (Sandbox Code Playgroud) 使用Apache Commons CSV库解析CSV文件时出现以下错误.
Exception in thread "main" java.io.IOException: (line 2) invalid char between encapsulated token and delimiter
at org.apache.commons.csv.Lexer.parseEncapsulatedToken(Lexer.java:275)
at org.apache.commons.csv.Lexer.nextToken(Lexer.java:152)
at org.apache.commons.csv.CSVParser.nextRecord(CSVParser.java:450)
at org.apache.commons.csv.CSVParser.getRecords(CSVParser.java:327)
at parse.csv.file.CSVFileParser.main(CSVFileParser.java:29)
Run Code Online (Sandbox Code Playgroud)
这个错误是什么意思?
我有一个大.csv文件(大约300 MB),从远程主机读取,并解析为目标文件,但我不需要将所有行复制到目标文件.在复制时,我需要从源读取每一行,如果它传递了一些谓词,则将该行添加到目标文件中.
我想Apache CSV(apache.commons.csv)只能解析整个文件
CSVFormat csvFileFormat = CSVFormat.EXCEL.withHeader();
CSVParser csvFileParser = new CSVParser("filePath", csvFileFormat);
List<CSVRecord> csvRecords = csvFileParser.getRecords();
Run Code Online (Sandbox Code Playgroud)
所以我不能用BufferedReader.根据我的代码,new CSVParser()应该为每一行创建一个实例,这看起来效率很低.
如何在上面的情况下解析单行(具有表的已知标题)?
我正在使用apache commons CSV来编写csv文件.我想坚持这个库.在我编写csv文件时,在生成文件的第一列中,它包含双引号作为引号字符,其他列按预期生成.
我真的想在这里摆脱双引号.请查找以下代码.
CSVFormat format = CSVFormat.DEFAULT;
FileWriter fw = new FileWriter("Temp.csv");
CSVPrinter printer = new CSVPrinter(fw, format);
String[] temp = new String[4];
for(int i=0; i<4; i++) {
if(i==1)
temp[0] = "#";
else
temp[0] = "";
temp[1] = "hello" + (i+1);
temp[2] = "";
temp[3] = "test";
Object[] temp1 = temp[]
printer.printRecord(temp1);
}
fw.close();
printer.close();
Run Code Online (Sandbox Code Playgroud)
"",hello1,测试
"#",hello2,测试
"",hello3,测试
"",hello4 ,,测试
我不想在每一行的开头都有引号字符.我只想要一个没有引号的空字符串,与第3列中的相同.任何人都可以帮忙吗?
在我的例子中,有效的CSV是用逗号或分号分隔的.我对其他库开放,但它需要是Java.通过Apache CSVParser API阅读,我唯一能想到的就是这样做看起来既低效又丑陋.
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(file));
CSVFormat csvFormat = CSVFormat.EXCEL.withHeader().withDelimiter(';');
CSVParser parser = csvFormat.parse( reader );
// now read the records
}
catch (IOException eee)
{
try
{
// try the other valid delimeter
csvFormat = CSVFormat.EXCEL.withHeader().withDelimiter(',');
parser = csvFormat.parse( reader );
// now read the records
}
catch (IOException eee)
{
// then its really not a valid CSV file
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法首先检查分隔符,或者可能允许两个分隔符?除了捕获异常之外,任何人都有更好的想法吗?
我一直在寻找过去2个小时来解决我的问题是徒劳的.我试图使用Apache commons读取CSV文件,我能够读取整个文件但我的问题是如何只提取数组中CSV的标题?
在某些时候,我的代码需要接触一个CSVRecord,而我无法想出一种方法来创建它的模拟版本。
这门课是最终的,所以它不能被嘲笑。构造函数是私有的,所以我无法创建它的实例。一种方法如何测试使用CSVRecord该类的代码?
现在唯一有效的解决方案是解析测试夹具以获取对象的实例。这是我最好的方法吗?
我想跳过第一行并使用第二行作为标题.
我正在使用apache commons csv中的类来处理CSV文件.
CSV文件的标题位于第二行,而不是第一行(包含坐标).
我的代码看起来像这样:
static void processFile(final File file) {
FileReader filereader = new FileReader(file);
final CSVFormat format = CSVFormat.DEFAULT.withDelimiter(';');
CSVParser parser = new CSVParser(filereader, format);
final List<CSVRecord> records = parser.getRecords();
//stuff
}
Run Code Online (Sandbox Code Playgroud)
我天真地想,
CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(;)
Run Code Online (Sandbox Code Playgroud)
会解决问题,因为它与withFirstRowAsHeader不同,我认为它会检测到第一行不包含任何分号而不是记录.它没有.我试图跳过第一行(CSVFormat似乎认为是标题)
CSVFormat format = CSVFormat.DEFAULT.withSkipHeaderRecord().withFirstRecordAsHeader().withDelimiter(;);
Run Code Online (Sandbox Code Playgroud)
但这也行不通.我能做什么?withFirstRowAsHeader和withFirstRecordAsHeader之间的区别是什么?
我正在尝试使用 Apache Commons CSV 将带有某些标题的 CSV 文件读入 Java 对象。但是,当我运行代码时,我得到以下例外:
Exception in thread "main" java.lang.IllegalArgumentException: Mapping for Color not found, expected one of [Color, Name, Price, House Cost, Rent, 1 House, 2 Houses, 3 Houses, 4 Houses, Hotel, Mortgage]
at org.apache.commons.csv.CSVRecord.get(CSVRecord.java:102)
at GameBoard.<init>(GameBoard.java:25)
at Game.main(Game.java:3)
Run Code Online (Sandbox Code Playgroud)
有人可以解释异常的来源吗?在我看来,Apache Commons 以某种方式与我的输入不匹配。是我有什么问题还是其他什么东西坏了?这是我的代码片段:
Reader in;
Iterable<CSVRecord> records = null;
try {
in = new FileReader(new File(Objects.requireNonNull(getClass().getClassLoader().getResource("Properties.csv")).getFile()));
records = CSVFormat.EXCEL.withFirstRecordAsHeader().parse(in);
} catch (IOException | NullPointerException e) {
e.printStackTrace();
System.exit(1);
}
for (CSVRecord record :
records) {
spaces.add(new …Run Code Online (Sandbox Code Playgroud) 我在Java中使用apache.commons.csv库.我正在使用以下代码从网页上读取CSV文件:
InputStream input = new URL(url).openStream();
Reader reader = new InputStreamReader(input, "UTF-8");
defaultParser = new CSVParser(reader, CSVFormat.DEFAULT);
excelParser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());
defaultParsedData = defaultParser.getRecords();
excelParsedData = excelParser.getRecords();
Run Code Online (Sandbox Code Playgroud)
但是,我在这个库中找不到一个方法可以轻松地将这个文件写入我的计算机,以便打开它并稍后从中读取.
我试过这段代码来保存文件.
String outputFile = savePath+".csv";
CSVPrinter csvFilePrinter = null;
CSVFormat csvFileFormat = CSVFormat.EXCEL.withHeader();
FileWriter fileWriter = new FileWriter(outputFile);
csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
for (CSVRecord csvRecord : excelParser) {
for(String dataPoint: csvRecord){
csvFilePrinter.print(dataPoint);
}
csvFilePrinter.print('\n');
}
fileWriter.flush();
fileWriter.close();
csvFilePrinter.close();
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用此代码读取文件时,没有打印出来:
InputStream input = new FileInputStream(cvsFilePath);
Reader reader …Run Code Online (Sandbox Code Playgroud)