我正在尝试编写已从文本文件中读取的数据,并将其写入另一个文本文件,以逗号分隔的格式。我需要知道代码是什么来得出这个结论。这是我需要帮助的地方。
示例:
原始数据如下所示:
Agnico-Eagle Mines
COM
008474108
28996843个
716800个
716800个
N / A
N / A
N / A
716800个
N / A
Agrium Inc.
COM
008916108
145739616个
1646617个
1646617个
N / A
N / A
N / A
1646617个
N / A
AuRico Gold Inc
COM
05155C105
504505个
62875个
62875个
N / A
N / A
N / A
62875个
N / A
这就是我希望数据在RichTextBox中的样子:
Agnico-Eagle Mines,COM,008474108,28996843,716800,716800,N / A,N / A ,, N / A,716800,N / A
Agrium Inc.,COM,008916108,145739616,1646617,1646617,N / A,N / A ,, N / A,1646617,N / A
AuRico Gold Inc,COM,05155C105,504505,62875,62875,N / A,N / A ,, N / A,62875,N / A
就这样你从原始文本数据中知道,我想读第一行,然后添加一个逗号然后读取第二行将它追加到第一行然后添加一个逗号,这就是第1行的第12行线。第12行的结尾没有逗号。然后该过程重新开始。
任何信息表示赞赏。
感谢。
以下是我到目前为止编写的代码。
private void button1_Click(object sender, EventArgs e)
{
using (StreamReader Reader = new StreamReader(@"C:\Original_Text_File.txt"))
{
while (!Reader.EndOfStream)
{
TextBox1.AppendText(Reader.ReadLine());
}
}
}
private void button2_Click(object sender, EventArgs e)
{
using (StreamWriter Writer = new StreamWriter(@"C:\Original_Text_File.txt"))
{
Writer.WriteLine(TextBox1.Text);
}
}
答案 0 :(得分:5)
这是我阅读数据的方式:
var sbText = new System.Text.StringBuilder(10000);
// Keeps track of your current position within a record
int wCurrLine = 0;
// Number of rows in the file that constitute a record
const int LINES_PER_ROW = 12;
using (StreamReader Reader = new StreamReader(@"C:\Original_Text_File.txt"))
{
while (!Reader.EndOfStream)
{
// If we are not on the first row in the record, add a comma
if (wCurrLine != 0)
{
sbText.Append(",");
}
// Add the text
sbText.Append(Reader.ReadLine());
// Increment our current record row counter
wCurrLine++;
// If we have read all of the rows for this record
if (wCurrLine == LINES_PER_ROW)
{
// Add a line to our buffer
sbText.AppendLine();
// And reset our record row count
wCurrLine = 0;
}
}
// When all of the data has been loaded, write it to the text box in one fell swoop
TextBox1.Text = sbText.ToString();
编辑:我刚刚意识到我没有完全回答原始问题:没有理由使用文本框,除非你想在写出结果之前看到结果。如果您不需要这样做,可以替换以下行:
TextBox1.Text = sbText.ToString();
使用:
using (StreamWriter Writer = new StreamWriter(@"C:\Original_Text_File.csv"))
{
Writer.Write(sbText);
}
(注意文件名中扩展名的更改)。
答案 1 :(得分:5)
假设 12 是输入文字的幻数,
var query = File.ReadLines("a.txt")
.Select((line,no) => new{line,no})
.GroupBy(x => x.no/12)
.Select(g => String.Join(",",g.Select(x => x.line)));
File.WriteAllLines("b.txt",query);
这适用于您的样本输入和预期输出....
答案 2 :(得分:1)
如果您只想使用differecnt方法从一个文件写入另一个文件,请尝试以下方法:
protected string TextProperty { get; set; }
private void button1_Click(object sender, EventArgs e)
{
using (StreamReader reader = new StreamReader(@"C:\Original_Text_File.txt"))
{
// read all content file to the string property on your form.
TextProperty = reader.ReadToEnd();
}
}
private void button2_Click(object sender, EventArgs e)
{
using (StreamWriter writer = new StreamWriter(@"C:\Original_Text_File.txt"))
{
// write all content of the property to the file.
writer.Write(TextProperty);
}
}
如果你想逐行分开或为每一行做一个特定的处理,你可以使用StringBuilder
类。
private void button1_Click(object sender, EventArgs e)
{
using (StreamReader reader = new StreamReader(@"C:\Original_Text_File.txt"))
{
StringBuilder builder = new StringBuilder();
while (reader.Peek() >= 0)
{
builder.AppendLine(reader.ReadLine());
}
}
}
并获取StringBuilder
的所有内容,只需调用ToString()
方法并将其写入文件:
private void button2_Click(object sender, EventArgs e)
{
using (StreamWriter writer = new StreamWriter(@"C:\Original_Text_File.txt"))
{
writer.Write(builder.ToString());
}
}
答案 3 :(得分:0)
创建一个List<String>
,读取每一行..然后String.Join使用,
作为分隔符在List中。当然,请在每行开头的列表中调用Clear
方法。
答案 4 :(得分:0)
注意 - 如果有一个标记(空白行),我将此作为如何执行此操作的示例,我在编写时错过了读取示例数据。
这样的东西应该有效,我没有测试,所以它可能有拼写错误:
注意:我找一个空白行(而不是计数)来知道我在记录的末尾。这样,如果添加另一个字段,代码就不会中断。
using (StreamReader Reader = new StreamReader(@"C:\Original_Text_File.txt"))
{
StringBuilder aLine;
boolean first = true;
while (!Reader.EndOfStream)
{
// read source line
string inLine = Reader.ReadLine();
// if length is zero append to test box (we have a blank line between records)
if (inLine.Length == 0)
{
TextBox1.AppendText(aLine.ToString());
first = true;
}
// add a comma if we are not the first
if (!first)
{
aLine.Append(",");
}
aLine.Append(inLine);
// next time we won't be first
first = false;
}
TextBox1.AppendText(aLine.ToString());
}
答案 5 :(得分:0)
private void button1_Click(object sender, EventArgs e)
{
using (StreamReader Reader = new StreamReader(@"C:\Original_Text_File.txt"))
{
var i = 0;
while (!Reader.EndOfStream)
{
if (i % 12 == 11)
{
//every 12 lines you read append new line instead of ,
TextBox1.AppendText(string.Format("{0}\n", Reader.ReadLine()));
}
else
{
TextBox1.AppendText(string.Format("{0},", Reader.ReadLine()));
}
i++;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
using (StreamWriter Writer = new StreamWriter(@"C:\Original_Text_File.txt"))
{
using (StringReader Reader = new StringReader(TextBox1.Text))
{
while (true)
{
var line = Reader.Readline()
if(line !=null)
{
Writer.WriteLine(Reader.Readline());
}
else
{
break;
}
}
}
}
}