Read and split line by line in text file - vb.net

I am trying to read a text file from my applications resources. For each line in this text file I want to split the text before and after the comma.
Each line in txt file looks like this:
-125.325235,4845636
My issue is that the function loops and does not end constantly repeating the for each statement
For Each Line As String In My.Resources.CompanyBases
MsgBox(My.Resources.CompanyBases.Split(","c).First)
MsgBox(My.Resources.CompanyBases.Split(","c).Last)
Next

Firstly, don't ever get a resource over and over like that. Those properties are not "live". Every time you get the property, the resource has to be extracted from your assembly. If you need to use the value multiple times, get the property once and assign it to a variable, then use that variable over and over.
Secondly, you're not getting a file. The whole point of resources is that they are not distinct files but rather data compiled into your assembly. It's just a String like any other. How would you usually split a String on line breaks?
Finally, you have a For Each loop with a loop control variable Line, yet you never use that variable inside the loop. It should be Line that you're splitting inside the loop, not the resource property containing all the lines.
For Each line In My.Resources.CompanyBases.Split({Environment.NewLine}, StringSplitOptions.None)
Dim fields = line.Split(","c)
Debug.WriteLine(fields(0))
Debug.WriteLine(fields(1))
Next
Note that, if you're using .NET Core, Split will accept a String as well as a String array.

Related

How to call one line at a time in LabVIEW read text file?

I would like to read just one line of text at a time using "Read from text file" function. After the passing of this line I would like to move on to the next line after the rest of the program iterates once. When I change the "Read from text file" function to "Read lines", I can no longer put an indicator on the front panel for text. How can I iterate one line at a time? How can I put an indicator on the front panel to display which line of text was read?
This what you want:
Open/Create/Replace File (with inputs open and read-only)
— inside While Loop:
Read From Text File (with Read Lines)
End While Loop on error from Read From Text File
--After While Loop:
Close File
You can process the data in the While loop, or index it and process outside.
Read the Text file
Use for loop, and add the shift register to it
Through the input shift register terminal, wire the text file output (Initial data)
In Front panel, Right Click >> String >> Additional String Function >> Search/Split String
Wire the Initial data (from shift register) to the input string terminal and Provide the necessary search string/ Character (In my case, I have used line feed constant)
Search/Split String has two output terminals; Substring before the match and match +rest of the String
Trime the whitespace and wire the match +rest of the String o/p to the o/p shift register; Take the desired o/p from Substring before the match terminal
Set the tunnel mode of the desired string as indexing so that each line in the file is indexed to an element in the array.
Note: N value of the for loop's iteration terminal is equal to the no of lines in the file.

Change selected ComboBox item, from text file string not working

Ok.
So I am working on a project, irrelevant, and I have a bunch (8) of ComboBoxes (they are in DropDownList mode) and there is 8 save files. I have them being imported and converted to strings:
Using class2 As New StreamReader(path & "SaveData/classdata/classdata2.NIC")
Dim fdcount1 As String
fdcount = class2.ReadToEnd()
MessageBox.Show(fdcount1)
hr2choice.SelectedItem = fdcount1
End Using
I already tested this, and it seems to be working.
(Test code I used:)
MessageBox.Show(fdcount1)
and it showed the value ("DiVita")
Despite this, when I tried setting the ComboBox value to this, it did not seem to work.
The ComboBox does have this value in it, and if I try this, it works:
hr2choice.SelectedItem = "DiVita"
For whatever reasons though, it does not work when I try doing it directly from the string.
Thanks for any help with this!
Nic
To answer this, I have to assume that the data in the text file is formatted as one line for each piece of data.
There seems to be a couple of issues with your code. fdcount is just declared as a string where it should be an array to make it easier to access each line that is read from the file. fdcount1 has no relationship to fdcount - it is a completely separate entity, so the data in fdcount1 is coming from somewhere else.
Rather than the above code, It's easier to use this
Dim fdcount() As String
fdcount = File.ReadAllLines("SaveData/classdata/classdata2.NIC")
MessageBox.Show(fdcount(1))
Note that fdcount is declared as an Array of String. The 2nd line does all the opening, reading into the array, and closing of the file.
You can then access each element of the array as shown in the 3rd line.

Pick a random line from a text file and store it in a variable (Python 3)

I am trying to code a program which reads a file, which will contain many words (one word per line), then selects a random line (word) from the file, so I am able to store it in a variable for me to use later on.
I don't really know where to start as I am not very experienced. Any help would be appreciated.
Well first you will need to open the file
file = open('filename.txt', 'w')
Then you need to read the file you can read each line into a list by doing words = file.readlines (this can also be done with a loop or in a number of other ways)
Then you can use the random module to generate a random number and get the word from that index in the words list. Then just store that word to a variable.
There are other ways of doing this but this is one of the easiest.

Cutting up a CSV file using split

I have a csv file, when i use the split function, my issue is that the 16th segment of the array has a name in it (in most cases) that has first and last name split by a comma. This obviously causes me issues as it puts my array out of sync. any suggestions on how i can handle this?
the string in the 16th segment is surrounded by "" if that helps, the split function still splits it though.
you can use TextFieldParser as indicated here
I recommend Lumen CSV Library, it can correctly handle field values with commas.
Also it has a very good performance, and a very simple usage.
See the link above, it won't disappoint you.
I think you're missing the point. Split is only good for simple csv parsing. Anything that gets even a little complicated means a lot of extra code. Something like the TextFieldParser is better suited to what you want. However if you must use Split here's one way:
Dim TempArray() As String
Dim Output As New List(Of String)
If SourceString.Contains("""") Then
TempArray = SourceString.Split(""""c)
Output.AddRange(TempArray(0).Split(","c))
Output.Add(TempArray(1))
'If the quoted part of the csv line is at the end of the line omit this statement.
Output.AddRange(TempArray(2).Split(","c))
Else
Output = New List(Of String)(SourceString.Split(","c))
End If
This assumes that the data is strictly organized, except for the quotes, if not you'll have to add validation code.
Split by "," with the quotes instead of just a comma. Don't forget to take care of the first and last quotes on the line.

How to edit a file in VB?

I have a CPP file. I am using VB in VS2005. I have opened that file using the FileSystemObject. I am reading each and every line in that CPP file. I have to comment all the lines until I encounter a return statement. I am using the scripting.textstream to read a line from the CPP file. But I have no idea as to how we can add a // comment to the beginning of every line that I read or even a multiline comment from the beginning till a return statement. Please help.
You seem to be using the FileSystemObject of the Windows script runtime instead of the System.IO.File class's methods. Strange!
The static System.IO.File.ReadAllLines() will read a file (and close it) and return a string array containing all the lines. You could then iterate through the array and add a comment to each line (except if the line starts with return).
Finally, save the changed text to the file using any of the WriteAllLines() method, thus overwriting any contained text.
Adding a multiline comment at the start would be even easier, you would not need to read the lines into an array.