如何在我的csv文件上显示列名 [英] how to display the column names on my csv file

查看:1043
本文介绍了如何在我的csv文件上显示列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已跳过列名称,以便更改行数据。使用 File.WriteAllLines 方法显示新文件后,新文件只显示没有列名的行数据。

I have the skipped the column names in order to make changes to the rows data. After using the File.WriteAllLines method to display the new file, the new file only display the row data without the column names.

任何建议都会感激。

public static IList<string> ReadFile(string fileName)
{
    var results = new List<string>();

    var target = File
        .ReadAllLines(fileName)
        .Skip(1) // Skip the line with column names
        .Select(line => line.Replace(' ', ',')); // ... splitting  pattern

    // Writing back to some other file
    File.WriteAllLines(fileName, target);

    return results;
}


推荐答案

只要不是第一个项目就替换

You could check for the index and replace only if it is not the first item

...
var target = File.ReadAllLines(fileName)
                 .Select((line, idx) => idx == 0 ? line : line.Replace(' ', ','));
...

这篇关于如何在我的csv文件上显示列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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