将文本附加到 CSV 文件中的行

发布于 2024-10-12 14:04:49 字数 366 浏览 1 评论 0原文

这个问题似乎已经在网络上被问了一百万次,但我找不到对我有用的答案。

基本上,我有一个 CSV 文件,其中有许多列(比如说两列)。该程序遍历 CSV 文件中的每一行,获取第一列值,然后要求用户将该值放置在第二列中。这是在运行 Windows 6 的手持设备上完成的。我正在使用 C# 进行开发。

这似乎是一件简单的事情。但我似乎无法在一行中添加文本。

我无法使用 OleDb,因为 System.Data.Oledb 不在我使用的 .Net 版本中。我可以使用另一个 CSV 文件,当他们完成每一行时,它会将其写入另一个 CSV 文件。但问题是 - 最后生成的文件需要包含每一行(所以如果他们将电池拉出一半怎么办)。如果他们回去,再次继续这样做,程序将如何知道从哪里开始。

This question seems to have been asked a million times around the web, but I cannot find an answer which will work for me.

Basically, I have a CSV file which has a number of columns (say two). The program goes through each row in the CSV file, taking the first column value, then asks the user for the value to be placed in the second column. This is done on a handheld running Windows 6. I am developing using C#.

It seems a simple thing to do. But I cant seem to add text to a line.

I cant use OleDb, as System.Data.Oledb isnt in the .Net version I am using. I could use another CSV file, and when they complete each line, it writes it to another CSV file. But the problems with that are - The file thats produced at the end needs to contain EVERY line (so what if they pull the batterys out half way). And what if they go back, to continue doing this another time, how will the program know where to start back from.

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

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

发布评论

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

评论(1

可爱暴击 2024-10-19 14:04:49

对于每一行,打开输出文件,将新行附加到其中,然后关闭输出文件。要重新启动,请计算上次运行中现有输出文件中的行数,这将为您提供输入文件中的起始位置(即跳过输入文件中的行数)。

编辑:在开始时,使用 System.IO.File.Copy 将输入文件复制到输出文件,以便在出现故障时拥有所有文件。现在打开输入文件,读取一行,对其进行转换,使用 File.ReadAllLines 将所有输出文件读入数组,在数组中的右侧索引处替换已更改的行,然后使用 File.WriteAllLines 写出新的输出文件。

像这样的东西:

string inputFileName = ""; // Use a sensible file name.
string outputFileName = ""; // Use a sensible file name.
File.Copy(inputFileName, outputFileName, true);
using (StreamReader reader = new StreamReader(inputFileName))
{
    string line = null;
    int inputLinesIndex = 0;
    while ((line = reader.ReadLine()) != null)
    {
        string convertedLine = ConvertLine(line);
        string[] outputFileLines = File.ReadAllLines(outputFileName);
        if (inputLinesIndex < outputFileLines.Length)
        {
            outputFileLines[inputLinesIndex] = convertedLine;
            File.WriteAllLines(outputFileName, outputFileLines);
        }
        inputLinesIndex++;
    }
}

For every row, open the output file, append the new row to it, and then close the output file. To restart, count the number of rows in the existing output file from the previous run, which will give you your starting in the input file (i.e., skip that number of rows in the input file).

Edit: right at the start, use System.IO.File.Copy to copy the input file to the output file, so you have all the file in case of failure. Now open the input file, read a line, convert it, use File.ReadAllLines to read ALL of the output file into an array, replace the line you have changed at the right index in the array, then use File.WriteAllLines to write out the new output file.

Something like this:

string inputFileName = ""; // Use a sensible file name.
string outputFileName = ""; // Use a sensible file name.
File.Copy(inputFileName, outputFileName, true);
using (StreamReader reader = new StreamReader(inputFileName))
{
    string line = null;
    int inputLinesIndex = 0;
    while ((line = reader.ReadLine()) != null)
    {
        string convertedLine = ConvertLine(line);
        string[] outputFileLines = File.ReadAllLines(outputFileName);
        if (inputLinesIndex < outputFileLines.Length)
        {
            outputFileLines[inputLinesIndex] = convertedLine;
            File.WriteAllLines(outputFileName, outputFileLines);
        }
        inputLinesIndex++;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文