将列表数组的内容输出到文本文件?
Output the contents of an array of lists to a text file?
我目前有一个列表数组,如下所示:
List<string>[] phase2 = new List<string>[200];
里面的一些列表是用字符串(每个 8 个)初始化的,如下所示:
phase2[0] = new List<string>() { "Bob", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes", };
phase2[1] = new List<string>() { "Joe", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes" };
phase2[2] = new List<string>() { "Bill", "Complex", "A", "AOT", "Yes", "Yes", "Yes", "Yes" };
我正在尝试将每个列表的内容写入一个 .txt 文件,并将 运行 放入我的知识块中...我希望每个 "list" 在 1 行上。我知道如何将数组写入文件和将列表写入文件,但不能同时将两者写入。我希望得到一些帮助来解决这个问题。
听起来您想使用 "nested" foreach 循环,但不清楚您是希望它们写入两个单独的文件,还是希望它们写入同一个文件。我会做第二个:
foreach (List<string> currentList in phase2)
{
foreach (string currentElement in currentList)
{
//change this out to whatever file output technique you use.
Console.Write currentElement + ",";
}
//starts a new line
Console.Write "\n"
}
这应该让您知道去哪里(假设您的意思是要以 columns/rows 样式编写每个列表的内容)。我几乎坚持使用定界符,除非你有特殊的理由不这样做。您可以使用任何字符,但坚持使用标准 (, ; | [tab] ) 来分隔您的列实际上只是确保与未来应用程序的兼容性。
如果您不需要分隔符,只需删除 +“,”部分即可。
用您的文件输出内容替换 Console.Write 命令。
请参阅下面的'ConcatToString'函数:
void Main()
{
List<string>[] phase2 = new List<string>[200];
phase2[0] = new List<string>() { "Bob", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes", };
phase2[1] = new List<string>() { "Joe", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes" };
phase2[2] = new List<string>() { "Bill", "Complex", "A", "AOT", "Yes", "Yes", "Yes", "Yes" };
File.WriteAllText(@"d:\scratch\arrOfWords.txt", ConcatToString(phase2));
}
public string ConcatToString(List<string>[] arrOfWords)
{
StringBuilder sb = new StringBuilder();
foreach(List<string> words in arrOfWords)
{
if(words?.Count > 0 ) sb.AppendLine(String.Join(",", words));
}
return sb.ToString();
}
正如@slightly-drifting 指出的那样,在遍历 2D 数据结构时,您需要使用嵌套循环。它可以是 foreach
、传统的 for
甚至 while
,具体取决于您的需要。
但是,如果您不关心内存分配或性能(仅处理 200 行时似乎就是这种情况),您可以使用 File and LINQ 来实现。请注意,LINQ 提供了更加用户友好的方法 API,但是对于这个问题,在幕后,它仍然使用嵌套循环。
够多的话。这是一个示例:
private static void WriteToFile(string filePath, List<string>[] content)
{
using (var fileWriter = new StreamWriter(filePath))
{
foreach (var line in content)
{
if (line == null)
{
continue;
}
for (var i = 0; i < line.Count; i++)
{
fileWriter.Write(line[i]);
if (i != line.Count - 1)
{
fileWriter.Write(',');
}
}
fileWriter.WriteLine();
}
}
}
private static void WriteToFileOneLiner(string filePath, List<string>[] content)
{
File.WriteAllLines(filePath, content.Where(line => line != null).Select(line => string.Join(',', line)));
}
public static void Main(string[] args)
{
List<string>[] phase2 = new List<string>[200];
phase2[0] = new List<string>() { "Bob", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes", };
phase2[1] = new List<string>() { "Joe", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes" };
phase2[2] = new List<string>() { "Bill", "Complex", "A", "AOT", "Yes", "Yes", "Yes", "Yes" };
WriteToFile(@"C:\Path\to\my\file.txt", phase2);
WriteToFileOneLiner(@"C:\Path\to\my\file_oneliner.txt", phase2);
}
我目前有一个列表数组,如下所示:
List<string>[] phase2 = new List<string>[200];
里面的一些列表是用字符串(每个 8 个)初始化的,如下所示:
phase2[0] = new List<string>() { "Bob", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes", };
phase2[1] = new List<string>() { "Joe", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes" };
phase2[2] = new List<string>() { "Bill", "Complex", "A", "AOT", "Yes", "Yes", "Yes", "Yes" };
我正在尝试将每个列表的内容写入一个 .txt 文件,并将 运行 放入我的知识块中...我希望每个 "list" 在 1 行上。我知道如何将数组写入文件和将列表写入文件,但不能同时将两者写入。我希望得到一些帮助来解决这个问题。
听起来您想使用 "nested" foreach 循环,但不清楚您是希望它们写入两个单独的文件,还是希望它们写入同一个文件。我会做第二个:
foreach (List<string> currentList in phase2)
{
foreach (string currentElement in currentList)
{
//change this out to whatever file output technique you use.
Console.Write currentElement + ",";
}
//starts a new line
Console.Write "\n"
}
这应该让您知道去哪里(假设您的意思是要以 columns/rows 样式编写每个列表的内容)。我几乎坚持使用定界符,除非你有特殊的理由不这样做。您可以使用任何字符,但坚持使用标准 (, ; | [tab] ) 来分隔您的列实际上只是确保与未来应用程序的兼容性。
如果您不需要分隔符,只需删除 +“,”部分即可。
用您的文件输出内容替换 Console.Write 命令。
请参阅下面的'ConcatToString'函数:
void Main()
{
List<string>[] phase2 = new List<string>[200];
phase2[0] = new List<string>() { "Bob", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes", };
phase2[1] = new List<string>() { "Joe", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes" };
phase2[2] = new List<string>() { "Bill", "Complex", "A", "AOT", "Yes", "Yes", "Yes", "Yes" };
File.WriteAllText(@"d:\scratch\arrOfWords.txt", ConcatToString(phase2));
}
public string ConcatToString(List<string>[] arrOfWords)
{
StringBuilder sb = new StringBuilder();
foreach(List<string> words in arrOfWords)
{
if(words?.Count > 0 ) sb.AppendLine(String.Join(",", words));
}
return sb.ToString();
}
正如@slightly-drifting 指出的那样,在遍历 2D 数据结构时,您需要使用嵌套循环。它可以是 foreach
、传统的 for
甚至 while
,具体取决于您的需要。
但是,如果您不关心内存分配或性能(仅处理 200 行时似乎就是这种情况),您可以使用 File and LINQ 来实现。请注意,LINQ 提供了更加用户友好的方法 API,但是对于这个问题,在幕后,它仍然使用嵌套循环。
够多的话。这是一个示例:
private static void WriteToFile(string filePath, List<string>[] content)
{
using (var fileWriter = new StreamWriter(filePath))
{
foreach (var line in content)
{
if (line == null)
{
continue;
}
for (var i = 0; i < line.Count; i++)
{
fileWriter.Write(line[i]);
if (i != line.Count - 1)
{
fileWriter.Write(',');
}
}
fileWriter.WriteLine();
}
}
}
private static void WriteToFileOneLiner(string filePath, List<string>[] content)
{
File.WriteAllLines(filePath, content.Where(line => line != null).Select(line => string.Join(',', line)));
}
public static void Main(string[] args)
{
List<string>[] phase2 = new List<string>[200];
phase2[0] = new List<string>() { "Bob", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes", };
phase2[1] = new List<string>() { "Joe", "Complex", "B", "AOT", "Yes", "Yes", "Yes", "Yes" };
phase2[2] = new List<string>() { "Bill", "Complex", "A", "AOT", "Yes", "Yes", "Yes", "Yes" };
WriteToFile(@"C:\Path\to\my\file.txt", phase2);
WriteToFileOneLiner(@"C:\Path\to\my\file_oneliner.txt", phase2);
}