Parsing a tab delimited text file with VB.Net - vb.net

I'm trying to parse a text file. First I plan on extrcating each line, then extracting each field by searching for a tasb.
When I use InStr to sercah for a \r (return for end of line) or \t (for tab) I always get a zero. But if I put in a visable letter such as a I got a 5.
also I tried /r, /n and \n all return a zero.
The file looks as follows:
ID Name
1 Patient
2 Bed
3 PatientSet
4 BedSet
5 TriggerSet
6 Triggering
7 Panel
the code
fileReader = My.Computer.FileSystem.ReadAllText("D:\BW\bwdatabase\ContextType.txt")
MsgBox(fileReader)
Str = fileReader
i = InStr(Str, "\r") // i uis zero
l = Mid(Str, 0, i - 1) // so this crashes becouse index is -1
MsgBo

Look into using the Microsoft.VisualBasic.FileIO.TextFieldParser class. It will do all the heavy lifting for you.

"\r" does not work in VB. For \r, use vbCr; for \n, use vbLf.

Related

Chunk string by max size and at a whitespace only in Kotlin

I'm trying to write a String to a file where the maximumg record size is allowed to be a fixed size (e.g. 5). Moreover it's only allowed to be chunked at a whitespace (which is used as a seperator).
So let's say that is my string:
var myString = "ac de defgh a b c ghiz xy"
and i want to chunk it by size of 5 and join it with a line seperator, so it should be like this:
ac de \n
defgh \n
a b c \n
ghiz \n
xy \n
I tried to do this: myString.chunked(5).joinToString(separator = "\n")
but it will not chunk at whitespace but anywhere after 5 chars.
Is there any other way to achieve what I'm trying to do?
One option is to split the string into words, group the words using a chunking function such as the one I gave in this answer, then rejoin the words into lines and the lines into a string:
myString.split(" ")
.chunkedBy(5 + 1){ length + 1 }
.joinToString("\n"){ it.joinToString(" ") }
This gives the required result.  (Except for the trailing space at the end of each line, and the trailing newline at the end; if you really need them they'd be easy enough to add.)  I hope you can see how it works.
This approach isn't terribly efficient; it creates lots of temporary objects (for the words, their lists, and the resulting lines, as well as the final string), but it probably scales reasonably well, and it's relatively clear and concise.  You could almost certainly write a function that performed better, but it would be more complex, and specific to this particular task.

How to add asterisk to a list of filenames and then make it a line using Notepad++

I have a list of file names (about 4000).
For example:
A-67569
H-67985
J-87657
K-85897
...
I need to put an asterisk before and after each file name. And then make it a line format.
Example:
*A-67569* *H-67985* *J-87657* *K-85897* so on...
Note that there is a space between filenames.
Forgot to mention, I'm trying to do this with Notepad++
How can I do it?
Please advise.
Thanks
C# example for list to string plus edits
List<string> list = new List<string> { "A - 67569"), "H-67985", "J-87657", "K-85897"};
string outString = "";
foreach(string item in list)
{
outString += "*" + item + "* ";
}
content of outstring: *A - 67569* *H-67985* *J-87657* *K-85897*
Use the Replace of your Notedad++ (Search > Replace..)
Select Extended (\n \r \t \0 \x...) on the bottom of the Replace window
In the field Find what write '\r\n' and in the field Replace with write * *
Replace all
Note, that you should manually place the single asterisk before the first and after the last words.
If this won't work, in step 2. instead of \r\n try to use only \n or \r.
You can use Regular expression in the search Mode.
Find what:
(\S+)(\R|$)
Replace with:
*$1
Note the space after de number one
For the archive
A-67569
H-67985
J-87657
K-85897
Output:
*A-67569 *H-67985 *J-87657 *K-85897
Explication of regex:
(\S+) Mean find one or more caracters is not a blank.
(\R|$) Mean find any end of line or end of file
(\S+)(\R|$) Mean find any gorup of caracters not blank ho end with end of line or end of file.
Explication of Replace with
When you use the $ simpbol, you are using a reference to the groups finded, $1 is the first group, in this case the group (\S+).

How to read elements from a line in VHDL?

I'm trying to use VHDL to read from a file that can have different formats. I know you're supposed to use the following two lines of code to read a line at a time, the read individual elements in that line.
readline(file, aline);
read(aline, element);
However my question is what will read(aline, element) return into element? What will it return if the line is empty? What will it return if I've used it let's say 5 times and my line only has 4 characters?
The reason I want to know is that if I am reading a file with an arbitrary number of spaces between valid data, how do I parse this valid data?
The file contains ASCII characters separated by arbitrary amounts of white space (any number of spaces, tabs, or new lines). If the line starts with a # that line is a comment and should be ignored.
Outside of these comments, the first part of the file contains characters that are only letters or numbers in combinations of variable size. In other words this:
123 ABC 12ABB3
However, the majority of the file (after a certain number of read words) will be purely numbers of arbitrary length, separated by an arbitrary amount of white space. In other words, the second part of the file is this:
255 0 2245 625 430
2222 33 111111
and I must be able to parse these numbers (and interpret them as such) individually.
As mentioned in the comments, all the read procedures in std.textio and ieee.std_logic_textio skip over leading spaces apart from the character and string versions (because a space is as much a character as any other).
You can test whether a line variable (the buffer) is empty like this:
if L'length > 0 then
where L is your line variable. There is also a set of overloaded read procedures with an extra status output:
procedure read (L : inout LINE;
VALUE: out <type> ;
GOOD : out BOOLEAN);
The extra output - GOOD - is true if the read was successful and false if it wasn't. The advantage of these if that the read is unsuccessful, the simulation does not stop (as it does with the regular procedures). Also, with the versions in std.textio, if the read is unsuccessful, the read is non-destructive (ie whatever you were trying to read remains in the buffer). This is not the case with the versions in ieee.std_logic_textio, however.
If you really do not know what format you are trying to read, you could read the entire line into a string, like this:
variable S : string(1 to <some big number>);
...
readline(F, L);
assert L'length < S'length; -- make sure S is big enough
S := (others => ' '); -- make sure that the previous line is overwritten
if L'length > 0 then
read(L, S(1 to L'length);
end if;
The line L is now in the string S. You can then write some code to parse it. You may find the type attribute 'value useful. This converts a string to some type, eg
variable I : integer;
...
I := integer'value(S(12 to 14));
would set integer I to the value contained in elements 12 to 14 of string S.
Another approach, as suggested by user1155120 below, is to peek at the values in the buffer, eg
if L'length > 0 then -- check that the L isn't empty, otherwise the next line blows up
if L.all(1) = '#' then
-- the first character of the line is a '#' so the line must be a comment

Write multiple lines to text file with '\n'

I have a program that iterates over all lines of a text file, adds spaces between the characters, and writes the output to the same file. However, if there are multiple lines in the input, I want the output to have separate lines as well. I tried:
let text = format!(r"{}\n", line); // Add newline character to each line (while iterating)
file.write_all(text.as_bytes()); // Write each line + newline
Here is an example input text file:
foo
bar
baz
And its output:
f o o\n b a r\n b a z
It seems that Rust treats "\n" as an escaped n character, but using r"\n" treats it as a string. How can I have Rust treat \n as a newline character to write multiple lines to a text file?
Note: I can include the rest of my code if you need it, let me know.
Edit: I am on Windows 7 64 bit
The problem is the 'r' in front of your string. Remove it and your program will print newlines instead of '\n'.
Also note that only most Unices use '\n' as newline. Windows uses "\r\n".

Need help understanding a basic Python script for parsing a file

Assume that the input file "input.txt" looks like the following:
Sam 92
Zoe 80
Ted 45
Sue 74
What is the output of the following code?
x = ""
infile = open("input.txt", "r")
for line in infile:
parts = line.split()
x += parts[1].strip()
print(x)
Lori, welcome to SO! Please read the community guidelines on how to use the different tools just above the text editor to make your question clearly readable by other users.
In regard to your question, let's break it down:
1 x = ""
2 infile = open("input.txt","r")
3 for line in infile.readlines():
4 parts = line.split()
5 x += parts[1].strip()
6 print x
Line 2 calls the open() method which creates a new object through which you can read a file (notice the "r" flag which means the file can only be read). Then, a for loop is executed for each line in the file (there's an error in line 3, I've added the readlines() method call for it to work).
For each line in the file, split the line using space as a delimiter (meaning that a list is created where each element is a substring between two spaces). Then, concatenate the second element in the list (this will be the number next to each name) to the variable x, stripping any unnecessary white spaces before and after the number. Then, when you're done, print x. This will print all the numbers, one after another, without any spacing, like so: 92804574