在读取CSV文件(C#)期间替换值并保存文件



我正在阅读csv文件:

string line;
StreamReader sr = new StreamReader(file.ToString());
while ((line = sr.ReadLine()) != null)
{
string col1 = line.Split(',')[10]; //old value
col1 = "my value"; //new value
}
sr.Close();
sr.Dispose();

我想用新价值替换旧值。

然后我需要保存包含更改的文件。

我该怎么做?

我建议使用File类而不是Streams和Readers.Linq在查询数据时非常方便:

var modifiedData = File
.ReadLines(file.ToString())
.Select(line => line.Split(',')) 
.Select(items => {
//TODO: put relevant logic here: given items we should return csv line
items[10] = "my value";
return string.Join(",", items);
})
.ToList(); // <- we have to store modified data in memory
File.WriteAllLines(file.ToString(), modifiedData);

另一种可能性(例如,当初始文件太长而无法容纳内存时)是将修改后的数据保存到临时文件中,然后Move它:

var modifiedData = File
.ReadLines(file.ToString())
.Select(line => line.Split(',')) 
.Select(items => {
//TODO: put relevant logic here: given items we should return csv line
items[10] = "my value";
return string.Join(",", items);
});
string tempFile = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.tmp");
File.WriteAllLines(tempFile, modifiedData);
File.Delete(file.ToString());
File.Move(tempFile, file.ToString());

一次读取整个文件会占用大量内存。更不用说创建其并行副本了。使用流可以修复它。试试这个:

void Modify()
{
using (var fs = new FileStream(file, FileMode.Open, FileAccess.ReadWrite))
{
string line;
long position;
while ((line = fs.ReadLine(out position)) != null)
{
var tmp = line.Split(',');
tmp[1] = "00"; // new value
var newLine = string.Join(",", tmp);
fs.WriteLine(position, newLine);
}
}
}

带扩展名:

static class FileStreamExtensions
{
private static readonly char[] newLine = Environment.NewLine.ToCharArray();
private static readonly int length = Environment.NewLine.Length;
private static readonly char eof = 'uFFFF';
public static string ReadLine(this FileStream fs, out long position)
{
position = fs.Position;
var chars = new List<char>();
char c;
while ((c = (char)fs.ReadByte()) != eof && (chars.Count < length || !chars.Skip(chars.Count - 2).SequenceEqual(newLine)))
{
chars.Add(c);
}
fs.Position--;
if (chars.Count == 0)
return null;
return new string(chars.ToArray());
}
public static void WriteLine(this FileStream fs, long position, string line)
{
var bytes = line.ToCharArray().Concat(newLine).Select(c => (byte)c).ToArray();
fs.Position = position;
fs.Write(bytes, 0, bytes.Length);
}
}

缺点是必须保持相同的长度。 例如999__9的长度均为 3。解决这个问题会使事情变得更加复杂,所以我会这样离开。

完整工作示例

相关内容

  • 没有找到相关文章

最新更新