473,804 Members | 3,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to parse a .txt file in C#

Hi all,

I have to parse a .txt file and retrieve only "Name" from second
column and store it in a list

My text file is of this format:

Bit Name Description
-1 Lucky groupa
-1 Roy groupb

In the list i should have only "Lucky" and "Roy" and nothing else.

I am very new to C# can anyone please tellme how i can do this

thanks for help,
Nithin
I
Nov 4 '08 #1
2 18635


<ni************ @gmail.comwrote in message
news:b1******** *************** ***********@x16 g2000prn.google groups.com...
Hi all,

I have to parse a .txt file and retrieve only "Name" from second
column and store it in a list

My text file is of this format:

Bit Name Description
-1 Lucky groupa
-1 Roy groupb

In the list i should have only "Lucky" and "Roy" and nothing else.

I am very new to C# can anyone please tellme how i can do this

thanks for help,
Nithin
I
This is actually pretty simple depending, but we need a little more
information. Is Bit always a -1 or a 0 or...always an integral value? Is
Name always a single word? Is Name ever surrounded with quotes? Could
there be any types of delimiters besides a space?

From the simple example you gave, you simply have 3 columns delimited by a
space...:

If you are using .Net 3.5, you can make use of anonymous types and LINQ
(note, I know this is definitely not the most efficient block of code in the
world, but hey, it works :)

string[] lines = File.ReadAllLin es(@"C:\input.t xt");
int i = 0;
var items =
from line in lines
where i++ != 0
select new {
Bit = line.Split(new char[] { ' ' }, 3)[0],
Name = line.Split(new char[] { ' ' }, 3)[1],
Description = line.Split(new char[] { ' ' }, 3)[2]
};

foreach (var item in items) {
Console.WriteLi ne(
"Bit: {0}, Name: {1}, Description: {2}",
item.Bit,
item.Name,
item.Descriptio n
);
}

// The following can be used in version 2.0 and does not make use of
anonymous types nor LINQ:

string[] lines = File.ReadAllLin es(@"C:\input.t xt");
int i = 0;

foreach (string line in lines) {
if (i++ == 0) continue;

string[] items = line.Split(new char[] { ' ' }, 3);
Console.WriteLi ne(
"Bit: {0}, Name: {1}, Description: {2}",
items[0], items[1], items[2]
);
}
HTH,
Mythran
Nov 4 '08 #2
StreamReader reader = new StreamReader(fi lePath);
string line;

//Assume that is a tab, if space change to " ".ToCharArr ay()
char[] splitChar = "\t".ToCharArra y();

while(null != (line = reader.ReadLine ))
{
string[] split = line.Split(spli tChar);

int bit= int.Parse(split[0]);
string name = split[1];
string description = split[2];

//Do something with parse here?
}

reader.Dispose( );

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

*************** *************** **************
| Think outside the box! |
*************** *************** **************
<ni************ @gmail.comwrote in message
news:b1******** *************** ***********@x16 g2000prn.google groups.com...
Hi all,

I have to parse a .txt file and retrieve only "Name" from second
column and store it in a list

My text file is of this format:

Bit Name Description
-1 Lucky groupa
-1 Roy groupb

In the list i should have only "Lucky" and "Roy" and nothing else.

I am very new to C# can anyone please tellme how i can do this

thanks for help,
Nithin
I
Nov 5 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2404
by: chuck amadi | last post by:
By the way list is there a better way than using the readlines() to > > >parse the mail data into a file , because Im using > > >email.message_from_file it returns > > >all the data i.e reads one entire line from the file , headers as well > > >as just the desired body messages . > > > > > >fp = file("/home/chuck/pythonScript/testbox") > > >mb = mailbox.UnixMailbox(fp, > > >email.message_from_file)
22
872
by: Ram Laxman | last post by:
Hi all, I have a text file which have data in CSV format. "empno","phonenumber","wardnumber" 12345,2234353,1000202 12326,2243653,1000098 Iam a beginner of C/C++ programming. I don't know how to tokenize the comma separated values.I used strtok function reading line by line using fgets.but it gives some weird behavior.It doesnot stripout the "" fully.Could any body have sample code for the same so that it will be helfful for my...
3
2655
by: slylos | last post by:
I've got a section of code in my app that keeps track of how much time an employee has accrued, which equals out to 1.33 days of PTO per month. I'm trying to store the time accrued so far in an XML file. I have no problems writing to and reading from the XML file and all my values are correct. The problem I'm having is my calculation never amounts to 1.33, it goes from 1.32999999999997 to 1.3743333333333. on the next run.
6
8604
by: trevor | last post by:
Incorrect values when using float.Parse(string) I have discovered a problem with float.Parse(string) not getting values exactly correct in some circumstances(CSV file source) but in very similar circumstances(XML file source) and with exactly the same value it gets it perfectly correct all the time. These are the results I got, XML is always correct, CSV are only incorrect for some of the values (above about 0.01) but always gives the...
5
64658
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
1
64212
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
11
3548
by: Peter Pei | last post by:
One bad design about elementtree is that it has different ways parsing a string and a file, even worse they return different objects: 1) When you parse a file, you can simply call parse, which returns a elementtree, on which you can then apply xpath; 2) To parse a string (xml section), you can call XML or fromstring, but both return element instead of elementtree. This alone is bad. To make it worse, you have to create an elementtree from...
2
3230
by: Lawrence Krubner | last post by:
Imagine a template system that works by getting a file, as a string, and then putting it through eval(), something like this: $formAsString = $controller->command("readFileAndReturnString", $formName); // 06-22-07 - the next commands try to import all the functions that the
6
2979
by: =?Utf-8?B?RGF2aWRN?= | last post by:
Hello, I have an XML file generated from a third party application that I would like to parse. Ideally, I plan on having a windows service setup to scan various folders for XML files and parse the file, then spit out totals. Since I haven't worked with XML too much in C#, I'm trying to develop a structured and easy-to-read way to parse the file. Essentially, I would like to read the file and add the "BatchTktAmountfor any...
5
2711
by: goldtech | last post by:
SAX XML Parse Python error message Hi, My first attempt at SAX, but have an error message I need help with. I cite the error message, code, and xml below. Be grateful if anyone can tell me what the fix is. Thanks.
0
9572
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10562
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10303
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6845
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.