首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >转换Dictionary<string,string>到CSV文件。键作为行1,值作为列的行值

转换Dictionary<string,string>到CSV文件。键作为行1,值作为列的行值
EN

Stack Overflow用户
提问于 2011-07-04 18:59:37
回答 3查看 4.8K关注 0票数 2

我有一个通过读取多个数据源创建的C#字典。字典包含键值对,其中键的值集合是逗号分隔的字符串值。

例如:

代码语言:javascript
复制
Dictionary<string, string> d = new Dictionary<string, string>();
    d.Add("cat", "2,2");
    d.Add("dog", "10, A");
    d.Add("llama", "A,B");
    d.Add("iguana", "-2,-3");

我希望最终的csv文件看起来像这样:

代码语言:javascript
复制
cat, dog, llama, iguana

2,10,A,-2
2,A,B,-3

我该如何实现这一点?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-07-04 19:48:24

如果您的数据结构是Dictionary,这将会更容易,否则您将需要提前拆分条目或在循环中多次拆分。列表也是可行的。根据您从数据源获取数据的方式,应该确定对传入的数据(例如,它已经是一个分隔字符串)执行String.Split()更容易,还是每个项都是单独添加的。

这段代码可以进行优化(例如,每次循环时都去掉字典查找)并进行清理,但应该可以让您开始,如果您的数据集不是太大,应该可以:

代码语言:javascript
复制
       static void Main(string[] args)
    {
        Dictionary<string, string[]> d = new Dictionary<string, string[]>();

        d.Add("cat", new string[] { "2", "2" });
        d.Add("dog", new string[] { "10", "A" });
        d.Add("llama", new string[] { "A", "B" });
        d.Add("iguana", new string[] { "-2", "-3" });

        // Not clear if you care about the order - this will insure that the names are in alphabetical order. 
        // The order of items in a dictionary are not guarenteed to be the same as the order they were added.
        var names = d.Keys.OrderBy(l => l).ToList();

        // Not clear if you know the number of items that will be in the list in advance - if not, find the max size 
        int maxSize = d.Values.Max(a => a != null ? a.Length : 0);

        Console.WriteLine(String.Join(", ", names));

        for (int i = 0; i < maxSize; i++)
        {
            foreach (string name in names)
            {
                string[] value = d[name];

                if ((value != null) && (i < value.Length))
                {
                    Console.Write(value[i]);
                }

                if (name != names.Last())
                {
                    Console.Write(",");
                }
            }

            Console.WriteLine();
        }
    }

将生成以下输出:

代码语言:javascript
复制
cat, dog, iguana, llama
2,10,-2,A
2,A,-3,B
票数 2
EN

Stack Overflow用户

发布于 2011-07-04 19:34:32

在foreach中使用的字典将返回一个KeyValuePair ...在这里你可以访问"Key“和"Value”。对于“值”的提取,您可以使用string.Split()。剩下的应该相对容易,这取决于你到底需要什么。

编辑,最后您只需打开一个文本文件进行写入,并以您想要的方式转储数据。

票数 0
EN

Stack Overflow用户

发布于 2011-07-04 20:22:12

代码语言:javascript
复制
Dictionary<string, List<string>> d = new Dictionary<string, List<string>>();    
    d.Add("cat", new List<string> {"2", "2"});    
    d.Add("dog", new List<string> {"10", "A"});    
    d.Add("llama", new List<string>{"A","B"});    
    d.Add("iguana", new List<string>{"-2","-3"});    

List<List<string>> values = d.Values.AsQueryable().ToList();    
int count = values[0].Count;    

for (int i = 0; i < count; i++)   
{  
    for (int j = 0; j < values.Count; j++)  
    {    
        Console.Write(values[j].ElementAt(i));  
    }    
}  

省略了检查和格式化,但这是您想要的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6570547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档