I have a tab delimited text file as so :
我有一个制表符分隔的文本文件,如下所示:
name \t loan period \t loan amount
John \t 5 years \t 6000
Sarah \t 5 years \t 6000
Jane \t 1 month \t 100
I'm looking to copy the lines where "loan period" = "5 years" to where "loan period" = "1 month", in order to show the comparison. The new lines would be appended at the end of the resulting file.
我希望将“贷款期限”=“5年”的行复制到“贷款期限”=“1个月”,以显示比较。新行将附加在结果文件的末尾。
The ultimate end result I hope to achieve is this :
我希望实现的最终结果是:
name \t loan period \t loan amount
John \t 5 years \t 6000
Sarah \t 5 years \t 6000
Jane \t 1 month \t 100
John \t 1 month \t 100
Sarah \t 1 month \t 100
I've been toying about this with Visual Basic .Net, and so far, this is what I've come up with
我一直在用Visual Basic .Net来解决这个问题,到目前为止,这就是我想到的
Dim strData As String
Dim i As Short
Dim strLine() As String
lngSize = 0
FileOpen(1, txtloanlistinput.Text, OpenMode.Input)
While Not EOF(1)
i = i + 1
strData = LineInput(1)
End While
FileClose(1)
ReDim loanlist(i)
strData = ""
lngSize = i
i = 0
FileOpen(2, txtloanlistinput.Text, OpenMode.Input)
While Not EOF(2)
i = i + 1
strData = LineInput(2)
If i = 1 Then
strData = LineInput(2)
End If
strLine = Split(strData, Chr(9))
loanlist(i).strName = strLine(0)
loanlist(i).strLoanPeriod = strLine(1)
loanlist(i).curLoanAmount = strLine(2)
End While
FileClose(1)
FileClose(2)
I'm drawing a blank as to how to proceed, and thought I'd ask for help.
我正在弄清楚如何继续,并认为我会寻求帮助。
2 个解决方案
#1
A litle ugly but is a start.
一个丑陋的丑陋但是一个开始。
//c#
OpenFileDialog dialog = new OpenFileDialog();
dialog.ShowDialog();
string filePath = dialog.FileName;
//in this list we store the match of 5 years.
List<string[]> fiveYears = new List<string[]>();
//open a reader.
StreamReader tr = new StreamReader(filePath);
//reaing 1° line.
string line=tr.ReadLine();
while (line != null && line != "" && line != "\n")
{
//split de line by tabs.
string[] lineByTabs = line.Split('\t');
//if the second term equals '5 years'
if (lineByTabs[1].Equals("5 years"))
{
//change from 5 years to 1 month, and to a lonan of 100.
lineByTabs[1] = "1 month";
lineByTabs[2] = "100";
fiveYears.Add(lineByTabs);
}
line = tr.ReadLine();
}
//close reader
tr.Close();
//open the file and apend lines.
TextWriter tw = new StreamWriter(filePath, true);
foreach (string[] lines in fiveYears)
{
tw.WriteLine(lines[0] + "\t" + lines[1] + "\t" + lines[2]);
}
tw.Close();
}
'vb.net
Dim dialog As OpenFileDialog = New OpenFileDialog
Dim filePath, line As String
Dim fiveYears As List(Of String()) = New List(Of String())
Dim lineByTabs As String()
Dim tr As StreamReader
Dim tw As TextWriter
dialog.ShowDialog()
filePath = dialog.FileName
tr = New StreamReader(filePath)
line = tr.ReadLine()
While Not line = ""
lineByTabs = line.Split(vbTab)
If lineByTabs(1).Equals("5 years") Then
lineByTabs(1) = "1 month"
lineByTabs(2) = "100"
fiveYears.Add(lineByTabs)
End If
line = tr.ReadLine()
End While
tr.Close()
tw = New StreamWriter(filePath, True)
For Each lines As String() In fiveYears
tw.WriteLine(lines(0) + vbTab + lines(1) + vbTab + lines(2))
Next
tw.Close()
hope it helps
希望能帮助到你
note: flie has to end in a new line. and folows the given format (3 colums).
注意:flie必须以新的一行结束。以及给定格式(3列)。
#2
One way of doing it:
一种方法:
Loop through the file once and put each name with 5 years into some array. Then use File.AppendText(path) to return a StreamWriter. Loop through your array of names and write it to the StreamWriter, then Flush and Close.
循环遍历文件一次,并将每个名称放入一些数组中5年。然后使用File.AppendText(path)返回StreamWriter。遍历您的名称数组并将其写入StreamWriter,然后刷新并关闭。
Check out File.AppendText documentation for example code.
查看File.AppendText文档以获取示例代码。
#1
A litle ugly but is a start.
一个丑陋的丑陋但是一个开始。
//c#
OpenFileDialog dialog = new OpenFileDialog();
dialog.ShowDialog();
string filePath = dialog.FileName;
//in this list we store the match of 5 years.
List<string[]> fiveYears = new List<string[]>();
//open a reader.
StreamReader tr = new StreamReader(filePath);
//reaing 1° line.
string line=tr.ReadLine();
while (line != null && line != "" && line != "\n")
{
//split de line by tabs.
string[] lineByTabs = line.Split('\t');
//if the second term equals '5 years'
if (lineByTabs[1].Equals("5 years"))
{
//change from 5 years to 1 month, and to a lonan of 100.
lineByTabs[1] = "1 month";
lineByTabs[2] = "100";
fiveYears.Add(lineByTabs);
}
line = tr.ReadLine();
}
//close reader
tr.Close();
//open the file and apend lines.
TextWriter tw = new StreamWriter(filePath, true);
foreach (string[] lines in fiveYears)
{
tw.WriteLine(lines[0] + "\t" + lines[1] + "\t" + lines[2]);
}
tw.Close();
}
'vb.net
Dim dialog As OpenFileDialog = New OpenFileDialog
Dim filePath, line As String
Dim fiveYears As List(Of String()) = New List(Of String())
Dim lineByTabs As String()
Dim tr As StreamReader
Dim tw As TextWriter
dialog.ShowDialog()
filePath = dialog.FileName
tr = New StreamReader(filePath)
line = tr.ReadLine()
While Not line = ""
lineByTabs = line.Split(vbTab)
If lineByTabs(1).Equals("5 years") Then
lineByTabs(1) = "1 month"
lineByTabs(2) = "100"
fiveYears.Add(lineByTabs)
End If
line = tr.ReadLine()
End While
tr.Close()
tw = New StreamWriter(filePath, True)
For Each lines As String() In fiveYears
tw.WriteLine(lines(0) + vbTab + lines(1) + vbTab + lines(2))
Next
tw.Close()
hope it helps
希望能帮助到你
note: flie has to end in a new line. and folows the given format (3 colums).
注意:flie必须以新的一行结束。以及给定格式(3列)。
#2
One way of doing it:
一种方法:
Loop through the file once and put each name with 5 years into some array. Then use File.AppendText(path) to return a StreamWriter. Loop through your array of names and write it to the StreamWriter, then Flush and Close.
循环遍历文件一次,并将每个名称放入一些数组中5年。然后使用File.AppendText(path)返回StreamWriter。遍历您的名称数组并将其写入StreamWriter,然后刷新并关闭。
Check out File.AppendText documentation for example code.
查看File.AppendText文档以获取示例代码。