写入/编辑CSV文件(不是重写整个文件!)

发布于 2024-10-29 00:33:23 字数 538 浏览 1 评论 0原文

我需要替换直接在 CSV 文件上操作的客户端的某些功能,该文件用作系统的配置文件。

搜索到的大多数案例都是关于从 CSV 读取到其他格式的。

其他将整个 CSV 放入内存,附加专用行和更改,然后将它们写回新文件(或覆盖现有文件)。

我想更聪明地完成这项任务,并寻求建议和代码想法。
我们可以假设每次只有一个用户有权访问该文件。
该文件使用 File.ReadLines() .NET 4 读取。

我的示例项目的一部分,

   var lines = from l in File.ReadLines("/Linq2Csv/data.csv")
               where l.Split(',')[0].StartsWith("Jonas") //Just test 'n pick 1 line
               select new
               {
                 Name = l.Split(',')[0],
                 ...

I'm in a situation to replace some functionalities from a client that operates directly on a CSV file, which used as a config file for a system.

Most of the cases available on search is about reading from CSV to other formats.

Other takes the whole CSV into memory, attaching the dedicated rows and changes, then write them back to a new file (or overwrite existing).

I would like to do this task smarter and ask for advices and code ideas.
We can presume that just one user per time, should have access to the file.
The file reads with File.ReadLines() .NET 4.

A part from my sample project,

   var lines = from l in File.ReadLines("/Linq2Csv/data.csv")
               where l.Split(',')[0].StartsWith("Jonas") //Just test 'n pick 1 line
               select new
               {
                 Name = l.Split(',')[0],
                 ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春夜浅 2024-11-05 00:33:23

CSV 文件只是一个连续的文本文件。

如果您想修改您拥有的内容以将整个文件读入内存,请在那里修改并再次写出。

假设一个更一般的文件包含以下内容:

ABCDEFGHIJKLMNOPQRSTUV

如果您想从中间删除“IJLKM”,您必须读取文件的其余部分(NOP...),以便您可以将其打乱以满足(...FGH)。

如果您想在“M”和“N”之间插入“0123456789”,您需要读取NZ,否则新字符将覆盖“NOPQRSTUVW”。

A CSV file is just a sequential text file.

If you want to modify the contents you have to read the entire file into memory, modify it there and write it out again.

Assume a more general case of a file that contains the following:

ABCDEFGHIJKLMNOPQRSTUV

If you want to remove "IJLKM" from the middle you have to read the remainder of the file (NOP...) so you can shuffle it up to meet (...FGH).

If you want to insert "0123456789" in between "M" and "N" you need to read N-Z otherwise the new characters will just overwrite "NOPQRSTUVW".

南七夏 2024-11-05 00:33:23

.NET Framework v4.0 附带了一个用于内存映射文件支持的新类,请参阅

  1. msdn
  2. 文章

您将有很好的机会让这项工作适合您。当线条长度发生变化时,您仍然会陷入要解决的问题。然而,如果您想要随机访问和虚假更新(例如,用“UNIX”替换“MSFT”),您将获得无与伦比的性能。

您可以采用填充策略在每个单元格/行中留有“备用空间”,以克服更改字段/行长度的问题。

一般来说,我认为 CSV 文件的成本不值得,但该技术时不时就会出现,值得一提。

干杯,
赛斯

The .NET Framework v4.0 comes with a new class for memorymapped file support, see

  1. msdn
  2. article

You will have a good chance of making this work for you. You will still be stuck with the problem to solve when linelenghts change. However, if you want random access and spurious updates (replacing "MSFT" by "UNIX", e.g.) you'll get awesome unrivalled performance.

You could employ padding strategies to have 'spare room' in each cell/line to overcome the problems with changing field/line lengths.

In general, I don't think it is worth the cost for CSV files, but the technique comes up every now and then and is worth mentioning.

Cheers,
Seth

ゃ人海孤独症 2024-11-05 00:33:23

在 .NET 中实现此目的的唯一方法是将其从 csv 文件移动到真正的数据库中。

您可以像打开数据库中的表一样打开文本文件。请参阅此处连接的 ConnectionString 示例: http://www.connectionstrings.com/textfile

但是,即使使用 .NET 将其视为数据库不会阻止底层代码在更新时覆盖整个文件。

文本文件不适合并发用户。这就是真正的数据库存在的原因。

The only way to accomplish this in .NET is to move it from a csv file into a true database.

You can open a text file as if it were a table in a database. See ConnectionString examples for connecting to it here: http://www.connectionstrings.com/textfile

However, even using .NET to treat it as a database won't stop the underlying code from overwriting the whole file when it's updated.

Text files just weren't meant for concurrent users. That's why true databases exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文