在 2d 或锯齿状数组中用逗号和新空格拆分字符串并在 C# 中访问项目
split string with comma and newspace in 2d or jagged array and access items in c#
你好,我有一个这样的文本文件,很多行 2 列
CF7CED1BF035345269118A15EF2D45A06, product1
CF7CED1BF035345269118A15EF2D45A09, product2
....
...
...
...
我需要拆分它并访问每个字段,更准确地说,我需要创建一个循环来创建许多文件,例如 product1.txt product2.txt 等,并将代码放在其左侧。
所以我需要用所有行的第 [2] 列的文件名创建文件,并将第 [1] 列作为每行的值括起来
我知道如何在数组中做基本的事情,比如读取所有行并存储它们,但我不知道如何制作一个循环来读取第 1 行的字段 1 然后 2,创建文件并存储(我知道如何读取和保存到文件)并继续下一行 2,然后再次进入字段 1 和字段 2,依此类推。
有人建议使用交错数组比二维数组更快
非常感谢。
"When you use ReadLines, you can start enumerating the collection of
strings before the whole collection is returned; when you use
ReadAllLines, you must wait for the whole array of strings be
returned before you can access the array. Therefore, when you are
working with very large files, ReadLines can be more efficient."
string path_read = @"c:\read\file.txt";
//Path to save resulting files.
string path = @"c:\temp\";
char[] comma = new char[1]{','};
//ASSUMPTION: Your every row has comma separated 2 values.
//Do a for loop.
//Code now use File.ReadLines
foreach (var currentLine in File.ReadLines(path_read))
{
string[] itemArray = currentLine.Split(comma, StringSplitOptions.RemoveEmptyEntries);
// Your item array now has 2 values from 2 columns in the same row.
// Do whatever with it.
File.WriteAllText(path+itemArray[1]+".txt", itemArray[0], Encoding.UTF8);
}
您是否需要保留内容以供进一步使用?如果目的是读取内容并将其保存到单独的文件中,则不需要单独的数组。
using (var reader = new StreamReader(@"input.txt"))
{
while (!reader.EndOfStream)
{
var inputText = reader.ReadLine();
var splitText = inputText.Split(',');
File.AppendAllLines(splitText[1] + ".txt", new List<string> {splitText[0]});
}
}
你好,我有一个这样的文本文件,很多行 2 列
CF7CED1BF035345269118A15EF2D45A06, product1
CF7CED1BF035345269118A15EF2D45A09, product2
....
...
...
...
我需要拆分它并访问每个字段,更准确地说,我需要创建一个循环来创建许多文件,例如 product1.txt product2.txt 等,并将代码放在其左侧。
所以我需要用所有行的第 [2] 列的文件名创建文件,并将第 [1] 列作为每行的值括起来
我知道如何在数组中做基本的事情,比如读取所有行并存储它们,但我不知道如何制作一个循环来读取第 1 行的字段 1 然后 2,创建文件并存储(我知道如何读取和保存到文件)并继续下一行 2,然后再次进入字段 1 和字段 2,依此类推。
有人建议使用交错数组比二维数组更快
非常感谢。
"When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient."
string path_read = @"c:\read\file.txt";
//Path to save resulting files.
string path = @"c:\temp\";
char[] comma = new char[1]{','};
//ASSUMPTION: Your every row has comma separated 2 values.
//Do a for loop.
//Code now use File.ReadLines
foreach (var currentLine in File.ReadLines(path_read))
{
string[] itemArray = currentLine.Split(comma, StringSplitOptions.RemoveEmptyEntries);
// Your item array now has 2 values from 2 columns in the same row.
// Do whatever with it.
File.WriteAllText(path+itemArray[1]+".txt", itemArray[0], Encoding.UTF8);
}
您是否需要保留内容以供进一步使用?如果目的是读取内容并将其保存到单独的文件中,则不需要单独的数组。
using (var reader = new StreamReader(@"input.txt"))
{
while (!reader.EndOfStream)
{
var inputText = reader.ReadLine();
var splitText = inputText.Split(',');
File.AppendAllLines(splitText[1] + ".txt", new List<string> {splitText[0]});
}
}