How do you output commas from a csv file - vb.net

I have created a program in visual studio 2010 which a user can input a sentence and then it will split the positions of the words and where they occur and the words. It then outputs them to two separate csv files.
If the user presses another button, it will regenerate the original inputted sentence.
Its seems to work with all types of punctuation apart from commas as they are the separator for csv files.
Is there a way of making the program output commas as well as other types of punctuation?
How i output to a CSV file:
Dim mydocpath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Using WordStore As System.IO.TextWriter = System.IO.File.CreateText(mydocpath + "\Output\Words.csv")
Using IndexStore As System.IO.TextWriter = System.IO.File.CreateText(mydocpath + "\Output\Outputs.csv")
How I read from a CSV file:
Using CSVFileToRead As New FileIO.TextFieldParser(mydocpath + "\Output\Outputs.csv")
The current problem involving commas:
Dim output As String = Nothing
For Each Word As String In UserInput.Split(CChar(" "))
output &= (words.IndexOf(Word) + 1).ToString & ", "
Next
Label1.Text = output.Substring(0, output.Length - 2)

Related

Minify HTML code into one line

My program is generating HTML code, which is placed afterwards into some string variable. This HTML code is ready to be placed in CSV file, so the entire code is surrounded by quotes, as well as all inner double quotes have additional quotes for escape. The result is the user can see it nicely formatted in output.
However, I have to convert this code to one line as assuming that my excel having trouble with this 'well formatted' HTML code as there are line-breaks. Therefore, I want before I place into CSV to make this HTML code into one line. Can you tell me how to achieve that?
You can replace line breaks by nothing to get a single lined output:
Dim TestString As String
TestString = " <body>" & vbCrLf & " some html" & vbCrLf & " </body>"
Dim SingleLined As String
SingleLined = Replace(TestString, vbCrLf, "")

VB.NET - Split text doc using blank lines

I have a text doc with multiple lines but each "subject" is separated by a blank line.. like..
Block 1
Line 1
Line 2
Block 2
Line 1
Line 2
And so on. I have tried lots of variants using vbcr and the like.. but can't get each block to be separated by the "blank lines". The goal is to use each block's data individually.
Any help or direction would be greatly appreciated. Thanks in advance.
I'm a little unclear on what you're trying to do: are you trying to parse each "block" of data separately, and need to recognize a blank line as a blank line?
If that is the case, you could read each line as follows:
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Dim tempString As String = ""
Do While objReader.Peek() <> -1
tempString = objReader.ReadLine().Trim()
if tempString.equals("") Then ' we have a blank line ...
Else
' Do something else with the tempstring line
End If
Loop
There may be more sophisticated ways to do this, but this is what I'd do.
Try regular Expressions.
Import System.Text.RegularExpressions 'may be needed in your file to use below code...
Dim blocks() As String = Regex.Split(myData, "\n[ \t]*\n")
' regex looks for the occurrence of an enter char "\n" following by an optional amount of whitespace "[ \t]*" following by another enter character
You may also need to intermix some "\r" in that regex as a "carriage return \r" and a "new line \n" are sometimes mixed in different ways in data.
"\r\n" = vbCrLf
"\r" = vbCr
"\n" = vbLf
Dim subjectsWithLines() as string=split(stringThatYouReadFromFile,chr(10))
Now there are different kinds of BLANK lines, if chr(10) doesn't work then try using chr(13) or Environment.newline
ChicagoMike's answer works too, but due different kind of "BLANK LINES", use
tempString.Count<1 instead equals

Reading and writing lines to and from a file

I am writing a simple console application using Microsoft Visual Basic 2010 express. I am trying to make a "newfile1.txt" file in which will be write something heading and than lets say 10 rows with three words in each row.
After that, I would like to read from file, and write to the "newfile2" file only the second word from file "newfile1.txt"
Than I would like to read from this file every line, and store lets and store only the second word from newfile1.txt
I try to use following code but I don't know how to specify several things (see bellow code)
Module Module1
Sub Main()
Dim i As Integer
FileOpen(1, "C:\Users\Namba\Documents\ANALYZA MD\newFile1.txt", OpenMode.Append, OpenAccess.ReadWrite, OpenShare.Default)
FileOpen(2, "C:\Users\Namba\Documents\ANALYZA MD\newFile2.txt", OpenMode.Append, OpenAccess.ReadWrite, OpenShare.Default)
WriteLine(1, "Heading of the file1")
For i = 1 To 10 Step 1
WriteLine(1, "Word 1" & "Word 2" & "Word 3")
Next
FileClose(1)
WriteLine(2, "Heading of the file2")
Dim filereader As System.IO.StreamReader
filereader = My.Computer.FileSystem.OpenTextFileReader("C:\Users\Namba\Documents\ANALYZA MD\newFile1.txt")
Dim stringReader As String
For i = 1 To 10 Step 1
stringReader = filereader.ReadLine()
WriteLine(2, stringReader)
Next
End Sub
End Module
So I have several questions:
Is it possible via ReadLine store words lets say in to the array or each word to the different string variable?
Is there simpler form how to open file and read each word, eventual define that the first word will be store in to the string var1, the second in to the var2 and so on, and similar if we have a file with numbers so that I would like to read from this file and store each number in to the some variable.
I can do this in fortran easy, via READ() WRITE() in very simple way
OPEN(UNIT=11, FILE="newfile1.txt)
READ(UNIT=11) x, y, z
OPEN(UNIT=12, FILE="newfile2.txt)
WRITE(UNIT=12,*) y
So this will read from one file the first 3 word(or number if the x, y, z is declare as number) from the first line and write in to the second file just second word (or number).
So I wonder if there is something very similar also in visual basic?
In general, if your files aren't large then it is faster (with respect to code writing) and easier to just read the contents of the file into memory and then manipulate it as needed.
Hopefully these examples will be of some help.
' Read entire contents of file1.txt into an array.
Dim file1 As String() = System.IO.File.ReadAllLines("C:\file1.txt")
' Now extract the 2nd word from each line (assuming all lines have at least 2 words).
Dim secondWords As New List(Of String)
For Each line In file1
' Break apart the string by spaces and take the second index (word).
secondWords.Add(line.Split(" ")(1))
Next
' Write the contents to a new file.
' This new file will have 1 word per line.
System.IO.File.WriteAllLines("C:\file2.txt", secondWords.ToArray())
If you are looking to examine each word, the code can become something like this:
' Read entire contents of file1.txt into an array.
Dim file1 As String() = System.IO.File.ReadAllLines("C:\file1.txt")
' Process each line.
For Each line In file1
' Process each word within the line.
For Each word In line.Split(" ")
' Do something with the word.
Console.WriteLine(word)
Next
' Or process by word index.
Dim words As String() = line.Split(" ")
For i As Integer = 0 To words.Length - 1
Console.WriteLine(String.Format("Word {0} is {1}", i + 1, words(i)))
Next
Console.WriteLine("Moving to a new line.")
Next

My visual basic program is creating files improperly and they won't run correctly through other programs

I have a visual basic program that creates files that are necessary for a semiweekly process. These files are a .bas file (for qbasic) and a .lot file (for voxco automation). I can live without the .bas file and simply put it's functionality directly into my program. I do, however, need the .lot file. Normally these files are copied from old files and edited manually. This is, as you can imagine, tedious. However, the files created by my program do not run properly through any means I have of running them. When I compare the manually created files to the automatically created files, the differences are minimal to nonexistent. The encoding doesn't seem to be an issue either. I simply don't know why the files created by my program are not running properly when the files created manually are working fine.
Here is the code that creates the .lot file:
Dim LotText As String
LotText = *removed*
Dim QuLines As String = Nothing
Dim Reader As New StreamReader(LotFilePath & OldStudy & ".LOT")
Dim SLine As String = Nothing
While Not Reader.EndOfStream
SLine = Reader.ReadLine()
If SLine.StartsWith("*QU") Then
QuLines = QuLines & SLine & vbCrLf
End If
End While
LotText = LotText & QuLines
Dim TempPath As String
TempPath = LotFilePath & "BackEnd\" & StudyID & ".LOT"
My.Computer.FileSystem.WriteAllText(TempPath, LotText, 0)
When you say the differences are minimal - what are they exactly!? A single character at the beginning of the file could be making the whole thing fail.
I have had problems in the past with writing vbCrLf to files in this manner; try the following code instead to see if it offers any improvement.
Dim LotText As String = *removed*
' Create a list of strings for the output data
Dim QuLines As New Collections.Generic.List(Of String)
Dim Reader As New StreamReader(Path.Combine(LotFilePath, OldStudy & ".LOT"))
Dim SLine As String = Nothing
While Not Reader.EndOfStream
SLine = Reader.ReadLine()
If SLine.StartsWith("*QU") Then
' This line is desired; add it to our output list
QuLines.Add(SLine)
End If
End While
' Concatenate the existing data and the output; uses the system defined NewLine
LotText &= Environment.NewLine & String.Join(Environment.Newline, QuLines)
Dim TempPath As String = Path.Combine(LotFilePath, "BackEnd", StudyID & ".LOT")
My.Computer.FileSystem.WriteAllText(TempPath, LotText, 0)

Creating a loop in StringBuilder to alter a text file

I would be grateful with some help with reading a text file into a Richtext box. The code I have at present appends the first line of text as I want it but the rest of the lines of text do not alter. I need a loop to read to the end of file and display in Richtext box. the code i have at present is this:-
Dim FILE_NAME As String = "C:\Test.txt"
Dim sr As New System.IO.StreamReader(FILE_NAME)
RichTextBox1.Text = sr.ReadToEnd
Dim sb As New System.Text.StringBuilder(RichTextBox1.Text)
sb.Insert(5, " ")
sb.Insert(12, " ")
sb.Insert(18, " ")
sb.Insert(25, " ")
sb.Insert(29, " ")
sb.Insert(32, " ")
sb.Insert(37, " ")
sb.Insert(44, " ")
sb.Insert(45, " ")
RichTextBox2.Text = sb.ToString
sr.Close()
I think you just want RichTextBox1.LoadFile "C:/test.txt"
that should be a backslash in the file name but my keyboard doesn't have one on this pc
The reason for the spaces is because each line of text that have the same length characters with spaces needs to be seperated to make it more readable.The original text looks like this:-
17915WHITE BLUE 001.900116A T123456111
72451BLACK ORANGE000.500208 B A123456123 'worst case
72455BLACK WHITE 002.703501 C123456124
Needs to look like below.
17915:WHITE BLUE :001.9:001:16:A :T:123456:111
72451:BLACK ORANGE:000.5:002:08: B :A:123456:123
72455:BLACK WHITE :002.7:035:01: :C:123456:124
I can produce the first line to a text file but i cannot reproduce the rest of the lines of text i think i need a loop to keep reading over the text file until the file is read.