C# 在C语言中读取文件并提取单词而不使用#的程序#
我读了一个文件,文件格式是这样的C# 在C语言中读取文件并提取单词而不使用#的程序#,c#,c#-4.0,C#,C# 4.0,我读了一个文件,文件格式是这样的 输入文件格式 id PosScore NegScore Word SynSet 00002098 0 0.75 unable#1 (usually followed by `to') not having the necessary means o
输入文件格式
id PosScore NegScore Word SynSet
00002098 0 0.75 unable#1 (usually followed by `to') not having the necessary means or skill or know-how; "unable to get to town without a car"; "unable to obtain funds"
00002312 0.23 0.43 dorsal#2 abaxial#1 facing away from the axis of an organ or organism; "the abaxial surface of a leaf is the underside or side facing away from the stem"
00002527 0.14 0.26 ventral#2 adaxial#1 nearest to or facing toward the axis of an organ or organism; "the upper side of a leaf is known as the adaxial surface"
00002730 0.45 0.32 acroscopic#1 facing or on the side toward the apex
00002843 0.91 0.87 basiscopic#1 facing or on the side toward the base
00002956 0.43 0.73 abducting#1 abducent#1 especially of muscles; drawing away from the midline of the body or from an adjacent part
00003131 0.15 0.67 adductive#1 adducting#1 adducent#1 especially of muscles; bringing together or drawing toward the midline of the body or toward an adjacent part
in this file
在这个文件中,Synset列应该是delete,其次,如果单词列有多个单词,那么id,PosScore,NegScore将根据一行中的单词repeat重复,但是id,PosScore,NegScore将是相同的。
我想要上述文件的以下输出输出
id PosScore NegScore Word
00002098 0 0.75 unable#1
00002312 0.23 0.43 dorsal#2
00002312 0.23 0.43 abaxial#1
00002527 0.14 0.26 ventral#2
00002527 0.14 0.26 adaxial#1
00002730 0.45 0.32 acroscopic#1
00002843 0.91 0.87 basiscopic#1
00002956 0.43 0.73 abducting#1
00002956 0.43 0.73 abducent#1
00003131 0.15 0.67 adductive#1
00003131 0.15 0.67 adducting#1
00003131 0.15 0.67 adducent#1
我编写了以下代码,但它给出了意外的结果
TextWriter tw = new StreamWriter("D:\\output.txt");
private void button1_Click(object sender, EventArgs e)
{
StreamReader reader = new StreamReader(@"C:\Users\Zia Ur Rehman\Desktop\records.txt");
string line;
String lines = "";
while ((line = reader.ReadLine()) != null)
{
String[] str = line.Split('\t');
String[] words = str[4].Split(' ');
for (int k = 0; k < words.Length; k++)
{
for (int i = 0; i < str.Length; i++)
{
if (i + 1 != str.Length)
{
lines = lines + str[i] + ",";
}
else
{
lines = lines + words[k] + "\r\n";
}
}
}
}
tw.Write(lines);
tw.Close();
reader.Close();
}
我简化了您的代码并使其正常工作。 它仍然缺乏验证,通过使用
StringBuilder
可以提高性能,尤其是通过将每一行写入文件而不是将其附加到字符串。它还缺少异常处理
using (TextWriter tw = File.CreateText(@"c:\temp\result.txt"))
using (StreamReader reader = new StreamReader(@"stackov1.txt"))
{
string line;
String lines = "";
while ((line = reader.ReadLine()) != null)
{
String[] str = line.Split('\t');
String[] words = str[3].Split(' ');
for (int k = 0; k < words.Length; k++)
{
lines = lines + str[0] + "\t" + str[1] + "\t" + str[2] + "\t" + words[k] + "\r\n";
}
}
tw.Write(lines);
}
使用(TextWriter tw=File.CreateText(@“c:\temp\result.txt”))
使用(StreamReader=newstreamreader(@“stackov1.txt”))
{
弦线;
字符串行=”;
而((line=reader.ReadLine())!=null)
{
字符串[]str=line.Split('\t');
String[]words=str[3]。拆分(“”);
for(int k=0;k
所以,现在可以使用了。经过长期的努力工作注意:如果在输入文件中没有使用正确的制表符。结果将是不正确的。不要忽视正确的标签
TextWriter tw = new StreamWriter("D:\\output.txt");
private void button1_Click(object sender, EventArgs e)
{
StreamReader reader = new StreamReader(@"C:\Users\Mohsin\Desktop\records.txt");
string line;
String lines = "";
while ((line = reader.ReadLine()) != null)
{
String[] str = line.Split('\t');
String[] words = str[3].Split(' ');
for (int k = 0; k < words.Length; k++)
{
for (int i = 0; i < 4; i++)
{
if (i + 1 != 4)
{
lines = lines + str[i] + "\t";
}
else
{
lines = lines + words[k] + "\r\n";
}
}
}
}
tw.Write(lines);
tw.Close();
reader.Close();
}
TextWriter tw=新的StreamWriter(“D:\\output.txt”);
私有无效按钮1\u单击(对象发送者,事件参数e)
{
StreamReader=新的StreamReader(@“C:\Users\Mohsin\Desktop\records.txt”);
弦线;
字符串行=”;
而((line=reader.ReadLine())!=null)
{
字符串[]str=line.Split('\t');
String[]words=str[3]。拆分(“”);
for(int k=0;k
什么是“意外”?你如何知道新专栏何时开始?@Magnus我现在编辑它,你可以see@GertArnold我现在编辑它,你可以看到意想不到的结果,除了str[4]
之外,一切都很好。我想应该是str[3]
我添加了编写器和读取器代码,所以现在我的示例已经完成(在需要初始化IO之前)。请发布详细信息如果itSir i add IO有问题,但它没有写入文件ZIA,请用数据文件的完整路径替换stackov1.txt。另外,请使用调试器检查代码的错误。
TextWriter tw = new StreamWriter("D:\\output.txt");
private void button1_Click(object sender, EventArgs e)
{
StreamReader reader = new StreamReader(@"C:\Users\Mohsin\Desktop\records.txt");
string line;
String lines = "";
while ((line = reader.ReadLine()) != null)
{
String[] str = line.Split('\t');
String[] words = str[3].Split(' ');
for (int k = 0; k < words.Length; k++)
{
for (int i = 0; i < 4; i++)
{
if (i + 1 != 4)
{
lines = lines + str[i] + "\t";
}
else
{
lines = lines + words[k] + "\r\n";
}
}
}
}
tw.Write(lines);
tw.Close();
reader.Close();
}