[RESOLVED] File read CSV format and write new file with one word in one line
I bumped into another difficulty.. I read the file (like CSV format) and, trying to write it in a single line. [Tried using For Loop , but, as i use Test1 lines.length, the output file only shows upto the old length]
for example :
| Test1.txt contains : |
My output ends up only with : |
Output file Testresult.txt should be : |
abc/wzed|
adc/wssf/ssed|
sdkf/effd/qefdc/efsfd|
efds/dfsd| |
abc|
wzed|
adc|
wssf|
ssed|
sdkf/effd/qefdc/efsfd|
efds/dfsd| |
abc|
wzed|
adc|
wssf|
ssed|
sdkf|
effd|
qefdc|
efsfd|
efds|
dfsd| |
Following is the part of coding... Please help...
and.. i know that i can replace everything with "/" or "|" but, this is the format i'm looking for... some character will be coming after "|"... So, it must contain split & "|" in the end...
Code:
lines = IO.File.ReadAllLines(TextBox1.Text)
For icnt As Integer = 0 To lines.Length - 1
Dim lncnt As Integer
If lines(icnt).Contains("|") Then
Dim strq As String() = lines(icnt).Split("|")
If strq(0).Contains("/") Then
Dim spl1 As String() = strq(0).Split("/")
For jcnt As Integer = 0 To spl1.Length - 1
Dim strtmp As String = spl1(jcnt) & "|" & strq(1)
lncnt = icnt + jcnt
Array.Resize(lines, lines.Length + 1)
For counter = lines.Length - 2 To lncnt Step -1
lines(counter + 1) = lines(counter)
Next
lines(lncnt) = strtmp
Next
lines(lncnt + 1) = ""
icnt = lncnt
lncnt = 0
Else
End If
Else
End If
Next
IO.File.WriteAllLines(TextBox2.Text, lines)
*Textbox1 & textbox2 get the location of the file...
Re: File read CSV format and write new file with one word in one line
You say that there will be some characters coming after the "|" but you don't show any and your output doesn't seem to account for any. What you need to do is actually describe EXACTLY what the rules are. One example is all well and good but if it doesn't demonstrate what to do in every possible case then it's really not too much use. We need ALL the rules.
Re: File read CSV format and write new file with one word in one line
Quote:
Originally Posted by
jmcilhinney
You say that there will be some characters coming after the "|" but you don't show any and your output doesn't seem to account for any. What you need to do is actually describe EXACTLY what the rules are. One example is all well and good but if it doesn't demonstrate what to do in every possible case then it's really not too much use. We need ALL the rules.
Very Sorry... The output should look like the following...
The example ::
| Test1.txt contains : |
Output file Testresult.txt should be : |
abc/wzed|cad
adc/wssf/ssed|sde
efds/dfsd|cfv |
abc|cad
wzed|cad
adc|sde
wssf|sde
ssed|sde
efds|cfv
dfsd|cfv |
Re: File read CSV format and write new file with one word in one line
Quote:
Originally Posted by
darkbb
and.. i know that i can replace everything with "/" or "|" but, this is the format i'm looking for... some character will be coming after "|"... So, it must contain split & "|" in the end...
Maybe something like?...
Code:
Dim lines = TextBox1.Lines
Dim results As New List(Of String)
For Each line In lines
Dim items = line.Split({"/", "|"}, StringSplitOptions.RemoveEmptyEntries)
For Each item In items
results.Add(item & "|")
Next
Next
Dim output As String = String.Join(Environment.NewLine, results)
Quote:
Originally Posted by
darkbb
Very Sorry... The output should look like the following...
Too late.
Re: File read CSV format and write new file with one word in one line
Quote:
Originally Posted by
Edgemeal
Maybe something like?...
Code:
Dim lines = TextBox1.Lines
Dim results As New List(Of String)
For Each line In lines
Dim items = line.Split({"/", "|"}, StringSplitOptions.RemoveEmptyEntries)
For Each item In items
results.Add(item & "|")
Next
Next
Dim output As String = String.Join(Environment.NewLine, results)
It throws a error at
Code:
Dim output As String = String.Join(Environment.NewLine, results)
Value of type 'System.Collections.Generic.List(Of String)' cannot be converted to '1-dimensional array of String'
Re: File read CSV format and write new file with one word in one line
Quote:
Originally Posted by
darkbb
Very Sorry... The output should look like the following...
One more try,
Code:
Dim lines = TextBox1.Lines
Dim results As New List(Of String)
For Each line In lines
If line.Length > 0 Then
Dim opt = line.Split("|"c)(1)
line = line.Replace("|" & opt, "")
Dim items = line.Split("/"c)
For Each item In items
results.Add(item & "|" & opt)
Next
End If
Next
Dim output As String = String.Join(Environment.NewLine, results)
Re: File read CSV format and write new file with one word in one line
Quote:
Originally Posted by
Edgemeal
One more try,
Code:
Dim lines = TextBox1.Lines
Dim results As New List(Of String)
For Each line In lines
If line.Length > 0 Then
Dim opt = line.Split("|"c)(1)
line = line.Replace("|" & opt, "")
Dim items = line.Split("/"c)
For Each item In items
results.Add(item & "|" & opt)
Next
End If
Next
Dim output As String = String.Join(Environment.NewLine, results)
Your code still says the same error as i mentioned in the comment #5...
So, I did some small changes to your code... and, it worked like charm...
Thank you so much edgemeal...
and, jmcilhinney - thanx for pointing out... otherwise, i would have not noticed mistake in my thread...
Code:
lines = IO.File.ReadAllLines(TextBox1.Text)
Dim results As New List(Of String)
For Each line In lines
If line.Length > 0 Then
Dim opt = line.Split("|"c)(1)
line = line.Replace("|" & opt, "")
Dim items = line.Split("/"c)
For Each item In items
results.Add(item & "|" & opt)
Next
End If
Next
lines = results.ToArray
IO.File.WriteAllLines(TextBox2.Text, lines)