本文整理汇总了Java中org.apache.commons.csv.CSVParser.parse方法的典型用法代码示例。如果您正苦于以下问题:Java CSVParser.parse方法的具体用法?Java CSVParser.parse怎么用?Java CSVParser.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.csv.CSVParser
的用法示例。
在下文中一共展示了CSVParser.parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseHintFile
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
private HashMap<String, Map<String, String>> parseHintFile(Integer exercise, Integer exercise_order) {
final HashMap<String, Map<String, String>> list;
final CSVParser parser;
final URL resource;
final CSVFormat csvFormat;
final Charset charset;
list = new HashMap<String, Map<String, String>> ();
try {
resource = ResourceHelper.getResource(BuenOjoFileUtils.GAME_RESOURCES_INPUT_DIR ,this.gamePath,this.setPath,exercise.toString(),exercise_order.toString(),"xy_pista.csv");
charset= FileEncodingDetectorHelper.guessEncodingAndGetCharset(resource);
csvFormat = CSVFormatHelper.getDefaultCSVFormat();
parser = CSVParser.parse(resource, charset, csvFormat);
for (CSVRecord record : parser )
list.put(record.get("id").trim() , (Map<String, String>)record.toMap());
}
catch (IOException e) {
log.error("Fail",e);
}
return list;
}
示例2: parse
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
private ArrayList<Map<String, String>> parse() {
final ArrayList<Map<String, String>> list;
final CSVParser parser;
final URL resource;
final CSVFormat csvFormat;
final Charset charset;
list = new ArrayList<Map<String, String>> ();
try {
resource = ResourceHelper.getResource(isFromGameResourceInput(),fileName);
charset= FileEncodingDetectorHelper.guessEncodingAndGetCharset(resource);
csvFormat = CSVFormatHelper.getDefaultCSVFormat();
parser = CSVParser.parse(resource, charset, csvFormat);
for (CSVRecord record : parser )
list.add((Map<String, String>)record.toMap());
}
catch (IOException e) {
log.error("Fail", e);
}
return list;
}
示例3: parse
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
private ArrayList<Map<String,String>> parse() {
ArrayList<Map<String,String>> list;
CSVParser parser;
URL resource;
CSVFormat csvFormat;
Charset charset;
list = new ArrayList<Map<String,String>> ();
try {
resource = ResourceHelper.getResource(isFromGameResourceInput(),fileName);
charset= FileEncodingDetectorHelper.guessEncodingAndGetCharset(resource);
csvFormat = CSVFormatHelper.getDefaultCSVFormat();
parser = CSVParser.parse(resource, charset, csvFormat);
for (CSVRecord record : parser )
list.add(record.toMap());
}
catch (IOException e) {
log.error("Fail", e);
}
return list;
}
示例4: parseDelimitedAreaFile
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
private Map<String,String> parseDelimitedAreaFile(Integer exercise, Integer exercise_order) {
Map<String,String> list;
CSVParser parser;
URL resource;
CSVFormat csvFormat;
Charset charset;
list = new HashMap<String,String>();
try {
resource =ResourceHelper.getResource(isFromGameResourceInput(),this.gamePath,this.setPath,exercise.toString(), exercise_order.toString(),"areaDelimitada.csv");
charset= FileEncodingDetectorHelper.guessEncodingAndGetCharset(resource);
csvFormat = CSVFormatHelper.getDefaultCSVFormat();
parser = CSVParser.parse(resource, charset, csvFormat);
for (CSVRecord record : parser )
list = record.toMap();
}
catch (IOException e) {
log.error("Fail",e);
}
return list;
}
示例5: importSheet
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
/**
* Imports a Sheet from a CSV file in the specified path.
* @param path a CSV File Path.
* @return a new Sheet or null if parsing failed
*/
public Sheet importSheet(String path) {
File csvData = new File(path);
// Parse the CSV file.
CSVParser parser;
try {
parser = CSVParser.parse(csvData, Charset.defaultCharset(), CSVFormat.RFC4180);
} catch (IOException e) {
return null;
}
// Create our new sheet.
Sheet sheet = new Sheet("Imported Sheet");
// Populate its cells.
for (CSVRecord record : parser) {
for (int x = 0; x < record.size(); ++x) {
sheet.setCellValue(x, (int) record.getRecordNumber() - 1, record.get(x), true);
}
}
return sheet;
}
示例6: main
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
CSVParser parser = CSVParser.parse(new File("dev/twitter-hate-speech-processed.csv"), Charset.forName("Cp1252"), CSVFormat.DEFAULT);
try (PrintWriter writer = new PrintWriter("training/bad/model_comments_bad_tweets.txt", "UTF-8")) {
boolean skipFirst = true;
for (CSVRecord r : parser) {
if (skipFirst){
skipFirst=false;
continue;
}
String classif = r.get(0);
if (classif.equalsIgnoreCase("The tweet is not offensive")) {
continue;
}
writer.println(r.get(2));
}
}finally {
parser.close();
}
}
示例7: invoke
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
public String invoke(String sender, String messageContent) {
try {
String command = messageContent;
List<String> commandArgs = new ArrayList<>();
if (messageContent.contains(" ")) {
command = substringBefore(messageContent, " ");
String args = substringAfter(messageContent, " ");
CSVParser csvRecords = CSVParser.parse(args, CSVFormat.newFormat(' ').withQuote('"'));
commandArgs = Lists.newArrayList(csvRecords.iterator().next().iterator());
commandArgs = commandArgs.stream()
.map((arg) -> arg.startsWith("$") ? store.get(sender, substringAfter(arg, "$"), String.class) : arg)
.collect(Collectors.toList());
}
return commands.get(command).invoke(sender, commandArgs);
} catch (Exception ex) {
throw new JanuszException(ex);
}
}
示例8: getResponse
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
/**
* Retrieves the answers of responseID for the survey identified by surveyID
*
* @param surveyID
* identifier of the survey
* @param responseID
* identifier of the response
* @return the answers of responseID for the survey identified by surveyID
* @throws ClientProtocolException
* @throws IOException
*/
public Hashtable<String, String> getResponse(int surveyID, int responseID)
throws ClientProtocolException, IOException {
String sessionKey = getSessionKey();
String responses = exportResponseByResponseID(sessionKey, surveyID,
responseID);
CSVParser csvParser = CSVParser.parse(responses, CSVFormat.RFC4180);
List<CSVRecord> csvRecordsList = csvParser.getRecords();
CSVRecord csvQuestions = csvRecordsList.get(0);
CSVRecord csvAnswers = csvRecordsList.get(1);
Iterator<String> iterQuestion = csvQuestions.iterator();
Iterator<String> iterAnswer = csvAnswers.iterator();
Hashtable<String, String> questionAnswerDictionary = new Hashtable<String, String>();
while (iterQuestion.hasNext()) {
String question = iterQuestion.next();
String answer = iterAnswer.next();
if (!isIgnoredKey(question))
questionAnswerDictionary.put(formatQuestion(question),
formatAnswer(answer));
}
return questionAnswerDictionary;
}
示例9: map
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
@Override
public void map(Text key, BytesWritable value, Context context)
throws IOException, InterruptedException {
ByteArrayInputStream inputStream = new ByteArrayInputStream(
value.getBytes());
int n = value.getLength();
byte[] bytes = new byte[n];
inputStream.read(bytes, 0, n);
CSVParser parser = CSVParser
.parse(new String(bytes), CSVFormat.DEFAULT);
try {
for (CSVRecord csvRecord : parser) {
for (String string : csvRecord) {
word.set(string);
context.write(word, one);
Counter counter = context.getCounter(
CountersEnum.class.getName(),
CountersEnum.INPUT_WORDS.toString());
counter.increment(1);
}
}
} catch (Exception e) {
}
}
示例10: decodeCsv
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
/**
* <p>
* decodeCsv.
* </p>
*
* @param input an array of byte.
* @param inputLength a int.
* @param csvFormat a {@link org.apache.commons.csv.CSVFormat} object.
* @return an array of byte.
* @throws java.io.IOException if any.
*/
public byte[][] decodeCsv(final byte[] input, final int inputLength, final CSVFormat csvFormat)
throws IOException
{
final byte[][] field = new byte[includeColumn.length][];
try (final CSVParser csvparser = CSVParser.parse(new String(input, 0, inputLength), csvFormat))
{
final CSVRecord csvrecord = csvparser.getRecords().get(0);
final Iterator<String> fields = csvrecord.iterator();
for (int fNum = 0; fields.hasNext(); fNum++)
{
final String fieldAsString = fields.next();
if (fNum >= includeColumn.length)
return field;
if (fNum < includeColumn.length)
if (includeColumn[fNum])
field[fNum] = fieldAsString.getBytes();
}
return field;
}
}
示例11: loadAnnotatedSample
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
public void loadAnnotatedSample(String sID, File file)
{
String fn = file.getName();
System.out.println("Loading result file: " + fn);
try {
List<EnrichmentAnnotation> sample = getSample(sID);
CSVParser parser = CSVParser.parse(file, Charset.forName("UTF-8")
, CSVFormat.EXCEL);
Iterator<CSVRecord> iter = parser.iterator();
if ( iter.hasNext() ) { iter.next(); }
while (iter.hasNext()) { loadAnnotation(sID, sample, iter.next()); }
}
catch (IOException e) {
System.err.println("Error loading file: " + fn);
}
}
示例12: exportToCsv
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
@Test
public void exportToCsv() throws E, IOException {
final R queryRequest = queryRequestBuilderFactory.getObject()
.queryRestrictions(testUtils.buildQueryRestrictions())
.queryType(QueryRequest.QueryType.MODIFIED)
.build();
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
exportService.exportQueryResults(outputStream, queryRequest, ExportFormat.CSV, Collections.emptyList(), 1001L);
final String output = outputStream.toString();
assertNotNull(output);
try (final CSVParser csvParser = CSVParser.parse(output, CSVFormat.EXCEL)) {
final List<CSVRecord> records = csvParser.getRecords();
assertThat(records, not(empty()));
final CSVRecord headerRecord = records.get(0);
assertThat(headerRecord.get(0), endsWith("Reference")); // byte-order mark may get in the way
assertEquals("Database", headerRecord.get(1));
final CSVRecord firstDataRecord = records.get(1);
final String firstDataRecordReference = firstDataRecord.get(0);
assertNotNull(firstDataRecordReference);
assertFalse(firstDataRecordReference.trim().isEmpty());
final String firstDataRecordDatabase = firstDataRecord.get(1);
assertFalse(firstDataRecordDatabase.trim().isEmpty());
}
}
示例13: readFromCsv
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
public static Collection<String[]> readFromCsv(String filename) {
try {
CSVParser parser = CSVParser.parse(Utilities.class.getResource(filename), StandardCharsets.UTF_8, CSVFormat.DEFAULT);
return parser.getRecords().stream()
.map(csvRecord -> new String[] {csvRecord.get(0), csvRecord.get(1) })
.collect(Collectors.toList());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例14: doParseDelimited
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
private static String[] doParseDelimited(String delimited, CSVFormat format) {
try (CSVParser parser = CSVParser.parse(delimited, format)) {
Iterator<CSVRecord> records = parser.iterator();
return records.hasNext() ?
StreamSupport.stream(records.next().spliterator(), false).toArray(String[]::new) :
EMPTY_STRING;
} catch (IOException e) {
throw new IllegalStateException(e); // Can't happen
}
}
示例15: parseFile
import org.apache.commons.csv.CSVParser; //导入方法依赖的package包/类
static void parseFile(final String fileStr, final PublishSubject<Map<String, String>> subject) throws Exception {
final CSVParser parser = CSVParser.parse(
new File(fileStr), StandardCharsets.UTF_8, CSVFormat.DEFAULT
);
try {
parser.forEach(csvRecord -> subject.onNext(toMap(csvRecord)));
subject.onComplete();
} finally {
parser.close();
}
}