编辑CSV文件(设计实现) [英] Editing CSV Files (Design Implementation)

查看:142
本文介绍了编辑CSV文件(设计实现)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始设计一个程序,它将根据类似的字符串及其已找到并保存到主CSV / Excel文件中的身份,自动查找和标识字符串的过程。

Im starting to design a program that will automate the process of finding and identifying strings correctly based on similar strings and their identities that have been found and saved into a master CSV/Excel file.

现在我想正确设计它,所以我在实现CSV / Excel读写部分时不会碰到问题。

Right now I want to design it properly so I dont run into issues later when implementing the CSV/Excel read writing part.

我可能会使用OpenCSV写入和读取文件,所以我的问题是更多关于如何编辑文件。

I will probably use OpenCSV to write and read the files, so my question is more about how I can edit the file.

上次处理编辑CSV文件时,我不得不重写每行到一个新的或现有的文件,而不仅仅是编辑一个特定的行。这是唯一的方法吗?

Last time I dealt with editing CSV files I had to rewrite each line to a new or existing file rather than just editing a specific line. Is this the only way to do this?

例外 - 如果我的csv是

Ex - if my csv is something like

1,2,3  and i wanted to change   1,2,3
4,5,6   4,5,6 to a,b,c          a,b,c
7,8,9                           7,8,9

唯一的方法是读取每一行,如果需要,然后再写出来?没有办法只是编辑中间行?

The only way would be to read each line, change it if needed, then write it out again? There's no way to just edit the middle line?

我问这个的原因是因为我计划通过GUI做很多自定义用户更改,并将更改写入一个文件每次可能会很糟糕?

The reason I ask this, is because I plan on doing a lot of custom user changes via GUI and writing the changes to a file every time would probably be very bad?

我认为在数组中保存每一行或单元格并编辑数组将是一个更有效的解决方案。

I think saving each line or cell in an array and editing the array would be a more efficient solution.

在编辑CSV文件时可以提供的任何技巧或建议?

Any tricks or advice you could offer when editing CSV files?

注意:我可能会在Java中这样做,因为我最熟悉的是使用Swing ,但我可以尝试用另一种语言。

Side note: I will probably be doing this in Java, as I am most familar with building GUI's with Swing, but I am open to trying it out in another language.

推荐答案

首先将问题分解为其组件过度复杂。

First off break the problem up into its components as you are overcomplicating it.

问题的根源是您有一个文件,其中包含您正在编写gui以允许用户编辑的记录。

The root of the problem is that you have a file with records that you are writing a gui for to allow the user to edit.

为了提高您要读取和写入同一文件的性能,尝试只读取或写入一个记录。

In an effort to increase performance you want to read and write to the same file, attempting to only read or write a single record.

有问题的文件采用csv格式。

The file in question is in a csv format.

所以第一个你冷的,所以没有必要过去。

So the first one you have down cold so there is no need to go over that.

我会说的第二部分与许多感叹号没有关系。原因是最糟糕的情况 - 你程序崩溃。在这一点,你已经损坏了你的原始。如果你知道记录的数量是小的,然后将整个内容读取到内存(说作为一个字符串列表),并解析各个字符串到他们的记录,当用户完成后,他们去保存你写入一个不同的文件一旦完成,删除原始文件并将第二个文件重命名为第一个文件。这样,如果你碰到最坏的情况下,你有原始文件完好无损或更改是在不同的名称。

The second part I would say do not do with many exclamation points. The reason for that is the worst case scenario - you program crashes. At which point you have corrupted your original. If you know the number of records are small then read the whole thing into memory (say as an list of strings) and parse the individual strings into their records and when the user is done and they go to save you write it into a different file that once done you delete the original and rename the second file to the first. This way if you hit the worst case scenario you either have the original file intact or the changes are there just under a different name.

如果一次有太多的内存容纳,则会出现 RandomAccessFile 允许读取和写入同一个文件。但
我建议你在开始时使用文件的副本(使用.tmp或.swp,一些编辑使用),并与它合作,因为它仍然保护你免受可怕的崩溃。

If there is too much to fit in memory at one time there is the RandomAccessFile that allows reads and writes to the same file. But I would recommend you make a copy of the file at the start (using the .tmp or .swp that some editors use) and work with that as it still protects you from the dreaded crash.

之后,它是如何处理CSV数据。如果是简单文本,可以使用Java String split方法。如果它更复杂,则openCSV具有 CSVParser 方法,将解析String为你的字符串数组。还有一个 CSVParserBuilder 可简化构建解析器的过程。

After that it is how you deal with CSV data. If it is simple text you can use the Java String split method. If it is more complex then openCSV has the CSVParser method that will parse the String into an array of strings for you. There is also a CSVParserBuilder that simplifies constructing the parser.

希望有帮助。

:)

这篇关于编辑CSV文件(设计实现)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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