什么是编辑csv文件的最佳方法 [英] what is the best way to edit csv file

查看:516
本文介绍了什么是编辑csv文件的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不完整的csv文件,需要准确更新,所以有这样的csv文件:

I have a incomplete csv file that needs to be accurately updated, so there is csv file like this :

one,two,three,four,five,six,seven,eight,nine,ten //This is a line(String)

自然地,文件要复杂得多,但是采用这种格式,这就是我要在7到8(或任何范围)之间插入new-word,new-word1, new-word3或n个单词的目的.我怎样才能做到这一点?

Naturally the file is far more complex but in this format, here is what I want to do insert new-word,new-word1, new-word3 or n words between seven and eight(or any range). How can I do that?

伪代码,代码或示例会很棒,我不知道如何开始.

Pseudo-code,code or example would be great, I don't have a clue how to even start.

更新:

也许我应该将其转换为数组或某种数据结构.然后在特定位置插入新项目,将其余内容右移,并在每次插入时都执行此操作.

Maybe I should convert this to array or some kind of datastructure. Then insert new item at the certain position, shift the rest of the content right and do that for each insertion.

我不知道是否正确的方法或如何开始对其进行编程

I don't know if is right way to go or how to begin programming it

更新II:

也许可以在列表中的csv中读取,将列表分为两个列表,第一个以7结尾.然后在第一个列表中添加n个单词,并在末尾加入两个列表?也不知道该如何编程

Maybe read in csv in the list, split list into two lists, first one ending with seven. Then add n words to first list and join two lists at the end? also don't know how to program this

推荐答案

看看 OpenCSV .

更新:以下是一些(很少)伪代码,可以大致了解如何使用OpenCSV:

UPDATE: Here's some (barely) pseudocode to give a general idea of how you would use OpenCSV for this:

CSVReader reader = new CSVReader(new FileReader("old.csv"));
CSVWriter writer = new CSVWriter(new FileWriter("new.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
    List<String> lineAsList = new ArrayList<String>(Arrays.asList(nextLine));
    // Add stuff using linesAsList.add(index, newValue) as many times as you need.
    writer.writeNext(lineAsList.toArray());
}

提示:@Mark Peters指出您无法更新Arrays.asList

Hat-tip: @Mark Peters who points out that you can't update the results of Arrays.asList

这篇关于什么是编辑csv文件的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆