CSV文字列をセル内改行を考慮して1行ずつに分離します。

@param csvText
CSV文字列
@return セル内改行を考慮した1行ずつを保持するList
962954
public static List<String> splitToLines(String csvText) {
        List<String> newLines = new ArrayList<>();

        for (Iterator<String> lineItr = Arrays.asList(csvText.split("\r\n|\n")).iterator(); lineItr
                .hasNext();) {

            String line = lineItr.next();
            LOG.trace("line : {}", line);
            StringBuilder newLine = new StringBuilder(line);

            // 行内にダブルクオーテーションが奇数個の場合はセル内改行ありとみなし、
            // 次にダブルクオーテーションが奇数個の行までを連結して1行とする。
            if (StringUtils.countMatches(line, '"') % 2 != 0) {

                do {
                    line = lineItr.next();
                    newLine.append("\n");
                    newLine.append(line);
                } while (StringUtils.countMatches(line, '"') % 2 == 0 && lineItr.hasNext());

            }

            String newLineStr = newLine.toString();
            LOG.trace("new line : {}", newLineStr);
            newLines.add(newLine.toString());
        }

        LOG.trace("actual line number : {}", newLines.size());
        return newLines;
    }
328708
def csvtolist(inputstr):
    
    reader = csv.reader([inputstr], skipinitialspace=True)
    output = []
    for r in reader:
        output += r
    return output
1208741
def read_csv(fpath):
    
    import csv
    import utool as ut
    #csvfile = open(fpath, 'rb')
    with open(fpath, 'rb') as csvfile:
        row_iter = csv.reader(csvfile, delimiter=str(','), quotechar=str('|'))
        row_list = [ut.lmap(ut.ensure_unicode, row) for row in row_iter]
    return row_list
1005774
private static String[] getSeriesDataFromCSVRows(File csvFile) {

    String[] xAndYData = new String[3];

    BufferedReader bufferedReader = null;
    try {
      int counter = 0;
      String line;
      bufferedReader = new BufferedReader(new FileReader(csvFile));
      while ((line = bufferedReader.readLine()) != null) {
        xAndYData[counter++] = line;
      }
    } catch (Exception e) {
      System.out.println("Exception while reading csv file: " + e);
    } finally {
      if (bufferedReader != null) {
        try {
          bufferedReader.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return xAndYData;
  }
435302
private <T> String serializeToCsv(T obj) {
	  if(obj == null) {
	        return "";
	  }
	  
	  for (Method method: obj.getClass().getMethods()) {
		  if ("java.util.List".equals(method.getReturnType().getName())) {
			  try {
		          @SuppressWarnings("rawtypes")
				  java.util.List itemList = (java.util.List) method.invoke(obj);
		          Object entry = itemList.get(0);
		          
		          List<String> stringList = new ArrayList<String>();
		          char delimiter = ',';
				  String lineSep = "\n";
	
				  CsvMapper mapper = new CsvMapper();
				  mapper.enable(JsonGenerator.Feature.IGNORE_UNKNOWN);
				  CsvSchema schema = mapper.schemaFor(entry.getClass());
				  for (int i = 0; i < itemList.size(); i++) {
					  if (i == 0) {
						  schema = schema.withHeader();
					  } else {
						  schema = schema.withoutHeader();
					  }
					  String csv = mapper.writer(schema
			                .withColumnSeparator(delimiter)
			                .withoutQuoteChar()
			                .withLineSeparator(lineSep)).writeValueAsString(itemList.get(i));
	
					  stringList.add(csv);
				  }
				  return StringUtil.join(stringList.toArray(new String[0]), "");
		  	  } catch (JsonProcessingException e) {
		          System.out.println(e);
		  	  } catch (IllegalAccessException e) {
		          System.out.println(e);
			} catch (IllegalArgumentException e) {
		          System.out.println(e);
			} catch (InvocationTargetException e) {
		          System.out.println(e);
			}
		  }
	  }
	  
	  return "";
  }
992866
private static byte[] writeCsv(String[] columnHeaders, String[][] rows) throws IOException {
    try (ByteArrayOutputStream csvStream = new ByteArrayOutputStream(); OutputStreamWriter streamWriter = new OutputStreamWriter(csvStream, Charset.forName("UTF-8"))) {
      CSVWriter csvWriter = new CSVWriter(streamWriter, ',');

      csvWriter.writeNext(columnHeaders);
      
      for (String[] row : rows) {
        csvWriter.writeNext(row);
      }

      csvWriter.close();
      
      return csvStream.toByteArray();
    }
  }
487682
public static final Set<String> fromCommaSeparatedValues(String csv) {
        if (csv == null || csv.isEmpty()) {
            return Collections.EMPTY_SET;
        }
        String[] tokens = csv.split(",");
        return new HashSet<String>(Arrays.asList(tokens));
    }
545939
public static List<PCMContainer> loadCSV(File file, PCMDirection pcmDir) throws IOException {

        CSVLoader csvL =
                new CSVLoader(
                new PCMFactoryImpl(),
                new CellContentInterpreter(new PCMFactoryImpl()), pcmDir);


        return csvL.load(file);
    }
1069207
public List<Term> seg(String text)
    {
        char[] charArray = text.toCharArray();
        if (HanLP.Config.Normalization)
        {
            CharTable.normalization(charArray);
        }
        if (config.threadNumber > 1 && charArray.length > 10000)    // 小文本多线程没意义，反而变慢了
        {
            List<String> sentenceList = SentencesUtil.toSentenceList(charArray);
            String[] sentenceArray = new String[sentenceList.size()];
            sentenceList.toArray(sentenceArray);
            //noinspection unchecked
            List<Term>[] termListArray = new List[sentenceArray.length];
            final int per = sentenceArray.length / config.threadNumber;
            WorkThread[] threadArray = new WorkThread[config.threadNumber];
            for (int i = 0; i < config.threadNumber - 1; ++i)
            {
                int from = i * per;
                threadArray[i] = new WorkThread(sentenceArray, termListArray, from, from + per);
                threadArray[i].start();
            }
            threadArray[config.threadNumber - 1] = new WorkThread(sentenceArray, termListArray, (config.threadNumber - 1) * per, sentenceArray.length);
            threadArray[config.threadNumber - 1].start();
            try
            {
                for (WorkThread thread : threadArray)
                {
                    thread.join();
                }
            }
            catch (InterruptedException e)
            {
                logger.severe("线程同步异常：" + TextUtility.exceptionToString(e));
                return Collections.emptyList();
            }
            List<Term> termList = new LinkedList<Term>();
            if (config.offset || config.indexMode > 0)  // 由于分割了句子，所以需要重新校正offset
            {
                int sentenceOffset = 0;
                for (int i = 0; i < sentenceArray.length; ++i)
                {
                    for (Term term : termListArray[i])
                    {
                        term.offset += sentenceOffset;
                        termList.add(term);
                    }
                    sentenceOffset += sentenceArray[i].length();
                }
            }
            else
            {
                for (List<Term> list : termListArray)
                {
                    termList.addAll(list);
                }
            }

            return termList;
        }
//        if (text.length() > 10000)  // 针对大文本，先拆成句子，后分词，避免内存峰值太大
//        {
//            List<Term> termList = new LinkedList<Term>();
//            if (config.offset || config.indexMode)
//            {
//                int sentenceOffset = 0;
//                for (String sentence : SentencesUtil.toSentenceList(charArray))
//                {
//                    List<Term> termOfSentence = segSentence(sentence.toCharArray());
//                    for (Term term : termOfSentence)
//                    {
//                        term.offset += sentenceOffset;
//                        termList.add(term);
//                    }
//                    sentenceOffset += sentence.length();
//                }
//            }
//            else
//            {
//                for (String sentence : SentencesUtil.toSentenceList(charArray))
//                {
//                    termList.addAll(segSentence(sentence.toCharArray()));
//                }
//            }
//
//            return termList;
//        }
        return segSentence(charArray);
    }
996897
public List<AnalyzedSentence> analyzeText(String text) throws IOException {
    List<String> sentences = sentenceTokenize(text);
    return analyzeSentences(sentences);
  }
649594
public static Matrix fromCSV(String csv) {
        StringTokenizer lines = new StringTokenizer(csv, "\n");
        Matrix result = DenseMatrix.zero(10, 10);
        int rows = 0;
        int columns = 0;

        while (lines.hasMoreTokens()) {
            if (result.rows() == rows) {
                result = result.copyOfRows((rows * 3) / 2 + 1);
            }

            StringTokenizer elements = new StringTokenizer(lines.nextToken(), ", ");
            int j = 0;
            while (elements.hasMoreElements()) {
                if (j == result.columns()) {
                    result = result.copyOfColumns((j * 3) / 2 + 1);
                }

                double x = Double.parseDouble(elements.nextToken());
                result.set(rows, j++, x);
            }

            rows++;
            columns = j > columns ? j : columns;
        }

        return result.copyOfShape(rows, columns);
    }
1187021
public CsvData read(Reader reader) throws IORuntimeException {
		final CsvParser csvParser = parse(reader);

		final List<CsvRow> rows = new ArrayList<>();
		CsvRow csvRow;
		while ((csvRow = csvParser.nextRow()) != null) {
			rows.add(csvRow);
		}

		final List<String> header = config.containsHeader ? csvParser.getHeader() : null;
		return new CsvData(header, rows);
	}
1221561
public List<String> getCSV(String name, boolean keepQuotes) {
        QuotedCSV values = null;
        for (HttpField f : this) {
            if (f.getName().equalsIgnoreCase(name)) {
                if (values == null)
                    values = new QuotedCSV(keepQuotes);
                values.addValue(f.getValue());
            }
        }
        return values == null ? Collections.emptyList() : values.getValues();
    }
592410
private Reuters21578 getReuters21578StoryFromText(String storyText) {

    // System.out.println(storyText);

    // ATTRIBUTES /////////////////////////
    String firstLine = storyText.split(System.getProperty("line.separator"))[0];
    // System.out.println(firstLine);
    String newIdString = extractAttribute(firstLine, "NEWID");
    int newId = Integer.parseInt(newIdString);
    String oldIdString = extractAttribute(firstLine, "OLDID");
    int oldId = Integer.parseInt(oldIdString);
    String topicsString = extractAttribute(firstLine, "TOPICS");
    boolean topicsBool = topicsString.equalsIgnoreCase("YES");
    String lewissplitString = extractAttribute(firstLine, "LEWISSPLIT");
    String cgisplitString = extractAttribute(firstLine, "CGISPLIT");

    // DATE /////////////////////////
    String dateString = extractTextBetweenTags(storyText, "DATE");
    // System.out.println("date=" + dateString);
    Date date = null;
    try {
      DateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss.SS");
      date = sdf.parse(dateString);
    } catch (ParseException e) {
      e.printStackTrace();
    }

    // TOPICS /////////////////////////
    String topicsText = extractTextBetweenTags(storyText, "TOPICS");
    List<String> topicsArray = extractElementAsLines(topicsText, "D");
    // System.out.println(topicsArray);
    String topicsCSV = Joiner.join(",", topicsArray);
    // System.out.println("topicsCSV: " + topicsCSV);
    // PLACES /////////////////////////
    String placesText = extractTextBetweenTags(storyText, "PLACES");
    List<String> placesArray = extractElementAsLines(placesText, "D");
    String placesCSV = Joiner.join(",", placesArray);
    // PEOPLE /////////////////////////
    String peopleText = extractTextBetweenTags(storyText, "PEOPLE");
    List<String> peopleArray = extractElementAsLines(peopleText, "D");
    String peopleCSV = Joiner.join(",", peopleArray);
    // ORGS /////////////////////////
    String orgsText = extractTextBetweenTags(storyText, "ORGS");
    List<String> orgsArray = extractElementAsLines(orgsText, "D");
    String orgsCSV = Joiner.join(",", orgsArray);
    // EXCHANGES /////////////////////////
    String exchangesText = extractTextBetweenTags(storyText, "EXCHANGES");
    List<String> exchangesArray = extractElementAsLines(exchangesText, "D");
    String exchangesCSV = Joiner.join(",", exchangesArray);
    // COMPANIES /////////////////////////
    String companiesText = extractTextBetweenTags(storyText, "COMPANIES");
    List<String> companiesArray = extractElementAsLines(companiesText, "D");
    String companiesCSV = Joiner.join(",", companiesArray);

    // TITLE /////////////////////////
    String titleText = extractTextBetweenTags(storyText, "TITLE");
    // DATELINE /////////////////////////
    String datelineText = extractTextBetweenTags(storyText, "DATELINE");
    // BODY /////////////////////////
    String body = extractTextBetweenTags(storyText, "BODY").replaceAll("\\s+", " ");
    // System.out.println(body);
    if (body.length() > maxBodyLength) {
      maxBodyLength = body.length();
    }

    Reuters21578 reuters21578 = new Reuters21578();
    reuters21578.setNewid(newId);
    reuters21578.setOldid(oldId);
    reuters21578.setTopicsbool(topicsBool);
    reuters21578.setLewissplit(lewissplitString);
    reuters21578.setCgisplit(cgisplitString);
    reuters21578.setDate(date);
    reuters21578.setTopics(topicsCSV);
    reuters21578.setPlaces(placesCSV);
    reuters21578.setPeople(peopleCSV);
    reuters21578.setOrgs(orgsCSV);
    reuters21578.setExchanges(exchangesCSV);
    reuters21578.setCompanies(companiesCSV);
    reuters21578.setTitle(titleText);
    reuters21578.setDateline(datelineText);
    reuters21578.setBody(body);
    // System.out.println(reuters21578.toString());

    return reuters21578;

  }
686229
public Iterable<String> toCSV() {
    checkState(!isClosed, WORKBOOK_CLOSED);
    Joiner joiner = Joiner.on(",").useForNull("");
    Iterable<String> CSVIterable =
        Iterables.transform(sheet, item -> joiner.join(rowToList(item, true)));
    return hasHeader ? Iterables.skip(CSVIterable, 1) : CSVIterable;
  }
189643
public String csvHeader() {

        if (m_csvItemFormatter != null) {
            return m_csvItemFormatter.csvHeader();
        } else {
            StringBuffer csv = new StringBuffer(1024);
            Iterator<CmsListColumnDefinition> itCols = m_columns.elementList().iterator();
            while (itCols.hasNext()) {
                CmsListColumnDefinition col = itCols.next();
                if (!col.isVisible()) {
                    continue;
                }
                csv.append(col.csvHeader());
                csv.append("\t");
            }
            csv.append("\n\n");
            return csv.toString();
        }
    }
1220195
public Csv lineDelimiter(String delimiter) {
		Preconditions.checkNotNull(delimiter);
		internalProperties.putString(FORMAT_LINE_DELIMITER, delimiter);
		return this;
	}