将两个文本文件合并为1

本文关键字:文件 合并 文本 两个 | 更新日期: 2023-09-27 17:54:04

我对c#还是个新手,对任何编程都很陌生,如果有任何帮助,我将不胜感激。

我有两个TXT文件,我想合并成一个格式如下:

text1.txt  
apple=1  
grape=2  
strawberry=3  
etc....
text2.txt  
1=156  
2=26  
3=180  
etc...

,我希望达到的格式是

Final.txt  
apple=156  
grape=26
strawberry=180  

我不太确定如何做到这一点,我正在考虑通过'='分割每行并使用if语句,但这似乎不起作用,好吧,我无法让它工作。

理想情况下,如果这可以在相同的按钮空白中完成,那就太好了。谁能给我指个正确的方向?

欢呼

将两个文本文件合并为1

将两个文件解析为dictionary:

对于每个文件,使用File.ReadAllLines获取一个字符串数组。

然后使用foreach循环遍历数组中的每个字符串。使用字符串。按照您的建议拆分每个字符串以获得一个键和一个值(例如,"apple"answers"1")。将键和值添加到字典中(每个文件一个字典)。

然后foreach通过text1的字典,并使用该值作为text2的字典的键。这可以让你映射"apple" -> 1 -> 156。

在循环遍历字典时,将每一行输出到final.txt。


例如:

    static void Main(string[] args)
    {
        String path1 = @"C:'file1.txt";
        String path2 = @"C:'file2.txt";
        String newFilePath = @"C:'final.txt";
        // Open the file1 to read from. 
        string[] readText = File.ReadAllLines(path1);
        // Add file1 contents to dictionary (key is second value)
        Dictionary<string, string> dictionaryA = new Dictionary<string, string>();
        foreach (string s in readText)
        {
            string[] parts = s.Split('=');
            dictionaryA.Add(parts[1], parts[0]);
        }
        // Open the file2 to read from. 
        readText = File.ReadAllLines(path2);
        // Add file2 contents to dictionaryB (key is first value)
        Dictionary<string, string> dictionaryB = new Dictionary<string, string>();
        foreach (string s in readText)
        {
            string[] parts = s.Split('=');
            dictionaryB.Add(parts[0], parts[1]);
        }

        // Create output file
        System.IO.StreamWriter file = new System.IO.StreamWriter(newFilePath);
        // write each value to final.txt file
        foreach (var key in dictionaryA.Keys)
        {
            if (dictionaryB.ContainsKey(key))
            {
                file.WriteLine(dictionaryA[key] + "=" + dictionaryB[key]);
            }
        }
        file.Close();
    }
  • 使用IO.File将文件1读入字典。ReadAllLines,按=分割,左边的都是键,右边的都是值。

  • 使用相同的方法将文件2读入字典

遍历第一个字典中的所有条目,并对每个值在另一个字典中使用TryGetValue。如果找到,输出一行到你的输出文件,取key1value2(第一个字典的键和第二个字典的值)-通过等号。

如果您想使用text1.txt作为键列表,使用text2.txt作为值列表,您可以简单地将第一个文件逐行读入字典(例如dictKeys),并将第二个文件读入另一个字典(dictValues)。然后,您可以遍历dictKey并将dictValues中的值拉到新字典中。

此时,只需循环遍历新字典并将其写入文件。

Dictionary<string, int> dictKeys = new Dictionary<string, int>();
Dictionary<int, int> dictValues = new Dictionary<int, int>();
using (var r = new StreamReader("text1.txt"))
{
    string line;
    while ((line = r.ReadLine()) != null)
    {
        var splitLine = line.Split(',');
        dictKeys.Add(splitLine[0], Convert.ToInt32(splitLine[1]));
    }
}
using (var r = new StreamReader("text2.txt"))
{
    string line;
    while ((line = r.ReadLine()) != null)
    {
        var splitLine = line.Split(',');
        dictValues.Add(Convert.ToInt32(splitLine[0]), Convert.ToInt32(splitLine[1]));
    }
}
Dictionary<string, int> dictCombined = new Dictionary<string, int>();
foreach (var item in dictKeys)
{
    var keyVal = item.Value;
    if (dictValues.ContainsKey(keyVal))
        dictCombined.Add(item.Key, dictValues[keyVal].Value);
}
using (var w = File.OpenWrite("Final.txt"))
{
    foreach (var item in dictCombined)
        w.WriteLine("{0}={1}", item.Key, item.Value);
}