473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Parsing a text file

JJ
Whats the best way for me to pull out records from a tab delimited text
file?

Or rather HOW do I parse the text, knowing that the tabs are field
delimiters and a return (I image) signifies a new record
?
JJ
Jun 6 '07 #1
22 2924
On Jun 6, 4:31 pm, "JJ" <a...@xyz.comwrote:
Whats the best way for me to pull out records from a tab delimited text
file?

Or rather HOW do I parse the text, knowing that the tabs are field
delimiters and a return (I image) signifies a new record
?
JJ
something like this

DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("column1"));
dt.Columns.Add(new DataColumn("column2"));

string[] lines = TextBox1.Text.Trim().Split('\r');
string[] s = null;

foreach (string line in lines)
{
DataRow row = dt.NewRow();

string[] fields = line.Split('\t');

s[0] = fields[0];
s[1] = fields[1];

row.ItemArray = s;
dt.Rows.Add(row);
}

GridView1.DataSource = dt;
GridView1.DataBind();

Jun 6 '07 #2
On Jun 6, 4:40 pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:
>
string[] fields = line.Split('\t');

s[0] = fields[0];
s[1] = fields[1];

row.ItemArray = s;
dt.Rows.Add(row);
Well... this should be optimized:

string[] fields = line.Split('\t');
row.ItemArray = fields;
dt.Rows.Add(row);

Jun 6 '07 #3
JJ
Great - thanks.
JJ
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11*********************@q66g2000hsg.googlegro ups.com...
On Jun 6, 4:31 pm, "JJ" <a...@xyz.comwrote:
>Whats the best way for me to pull out records from a tab delimited text
file?

Or rather HOW do I parse the text, knowing that the tabs are field
delimiters and a return (I image) signifies a new record
?
JJ

something like this

DataTable dt = new DataTable();

dt.Columns.Add(new DataColumn("column1"));
dt.Columns.Add(new DataColumn("column2"));

string[] lines = TextBox1.Text.Trim().Split('\r');
string[] s = null;

foreach (string line in lines)
{
DataRow row = dt.NewRow();

string[] fields = line.Split('\t');

s[0] = fields[0];
s[1] = fields[1];

row.ItemArray = s;
dt.Rows.Add(row);
}

GridView1.DataSource = dt;
GridView1.DataBind();

Jun 6 '07 #4
JJ
Actually, another question:
Could I open the text file from a path to the users file on their computer,
or do I have to (or is it best to) upload the text file first?
JJ
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11*********************@g4g2000hsf.googlegrou ps.com...
On Jun 6, 4:40 pm, Alexey Smirnov <alexey.smir...@gmail.comwrote:
>>
string[] fields = line.Split('\t');

s[0] = fields[0];
s[1] = fields[1];

row.ItemArray = s;
dt.Rows.Add(row);

Well... this should be optimized:

string[] fields = line.Split('\t');
row.ItemArray = fields;
dt.Rows.Add(row);

Jun 6 '07 #5
On Jun 6, 4:49 pm, "JJ" <a...@xyz.comwrote:
or do I have to (or is it best to) upload the text file first?
Yes, user should upload the file.

Another way is to use a TextBox Control where user could copy/paste an
entire content of the file

Jun 6 '07 #6
JJ
I'd prefer it if they could paste as I'd not have to worry about storage
space, but the files in question will have around 3000 lines, so I'm not
sure if that is going to be possible/practical...?
JJ
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
On Jun 6, 4:49 pm, "JJ" <a...@xyz.comwrote:
>or do I have to (or is it best to) upload the text file first?

Yes, user should upload the file.

Another way is to use a TextBox Control where user could copy/paste an
entire content of the file

Jun 6 '07 #7
On Jun 6, 8:31 pm, "JJ" <a...@xyz.comwrote:
possible
yes
practical...?
don't know :-) but it can be a good option, I think.

Jun 6 '07 #8
JJ
I guess to avoid my worry of too many files being uploaded, I could just get
them to choose the file on their computer, then read the file and store it
in a static filename (so it keeps over-writing the old one); as I won't need
the file after I've parsed it (and input the fields into a database), that
should be ok.

Hang on though - that wouldn't work if someone else tried to do another
upload at exactly the same time.

I'm not sure their going to like copying and pasting such large files. I
agree though, I'd much rather copying and pasting myself - a lot simpler.
It looks like its either that or an ever growing number of files that I
haven't given them permission to delete.

JJ

"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
On Jun 6, 8:31 pm, "JJ" <a...@xyz.comwrote:
>possible

yes
>practical...?

don't know :-) but it can be a good option, I think.

Jun 6 '07 #9
On Jun 6, 9:00 pm, "JJ" <a...@xyz.comwrote:
I guess to avoid my worry of too many files being uploaded, I could just get
them to choose the file on their computer, then read the file and store it
in a static filename (so it keeps over-writing the old one); as I won't need
the file after I've parsed it (and input the fields into a database), that
should be ok.

Hang on though - that wouldn't work if someone else tried to do another
upload at exactly the same time.

I'm not sure their going to like copying and pasting such large files. I
agree though, I'd much rather copying and pasting myself - a lot simpler.
It looks like its either that or an ever growing number of files that I
haven't given them permission to delete.
A copy/paste approach is good when you have an Excel file and you can
open it, modify and copy the entire contents directly from Excel to
the web site (with a TextBox) without saving the file as a tab-
delimeted (or CSV) export and doing an upload.

But if you already have such file the way with upload is fast and
secure...

Jun 6 '07 #10
JJ
Yes - it will be an excel file they'll have. I guess 3000 lines is possible
to cut and paste....
I'm not sure what the limitations are in terms of memory within excels
clipboard.

I would have though 3000 lines was well within limits, would you?
JJ

"Alexey Smirnov" <al************@gmail.comwrote in message
news:11*********************@g37g2000prf.googlegro ups.com...
On Jun 6, 9:00 pm, "JJ" <a...@xyz.comwrote:
>I guess to avoid my worry of too many files being uploaded, I could just
get
them to choose the file on their computer, then read the file and store
it
in a static filename (so it keeps over-writing the old one); as I won't
need
the file after I've parsed it (and input the fields into a database),
that
should be ok.

Hang on though - that wouldn't work if someone else tried to do another
upload at exactly the same time.

I'm not sure their going to like copying and pasting such large files. I
agree though, I'd much rather copying and pasting myself - a lot simpler.
It looks like its either that or an ever growing number of files that I
haven't given them permission to delete.

A copy/paste approach is good when you have an Excel file and you can
open it, modify and copy the entire contents directly from Excel to
the web site (with a TextBox) without saving the file as a tab-
delimeted (or CSV) export and doing an upload.

But if you already have such file the way with upload is fast and
secure...

Jun 6 '07 #11
On Jun 6, 9:33 pm, "JJ" <a...@xyz.comwrote:
Yes - it will be an excel file they'll have. I guess 3000 lines is possible
to cut and paste....
I'm not sure what the limitations are in terms of memory within excels
clipboard.

I would have though 3000 lines was well within limits, would you?
JJ
The Office clipboard is limited, but the Windows clipboard depends on
the RAM only.

I think, you should test if users will not have any problems with copy
and paste. I think 3000-lines-file is big, but not because of its size
in bytes but because you cannot be sure that you have pasted the whole
file in the small textbox area on the form. Though you can show the
number of submitted lines using a client js, and/ or on a postback

Jun 6 '07 #12
JJ
What method should I use to sort/edit/delete the data, in case the user
wants to adjust the data before inputting it to the database?

JJ

"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
On Jun 6, 9:33 pm, "JJ" <a...@xyz.comwrote:
>Yes - it will be an excel file they'll have. I guess 3000 lines is
possible
to cut and paste....
I'm not sure what the limitations are in terms of memory within excels
clipboard.

I would have though 3000 lines was well within limits, would you?
JJ

The Office clipboard is limited, but the Windows clipboard depends on
the RAM only.

I think, you should test if users will not have any problems with copy
and paste. I think 3000-lines-file is big, but not because of its size
in bytes but because you cannot be sure that you have pasted the whole
file in the small textbox area on the form. Though you can show the
number of submitted lines using a client js, and/ or on a postback

Jun 7 '07 #13
JJ
What I mean is how would I temporarily hold the data whilst any
sorting/deleting/editing can take place?
"JJ" <ab*@xyz.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
What method should I use to sort/edit/delete the data, in case the user
wants to adjust the data before inputting it to the database?

JJ

"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
>On Jun 6, 9:33 pm, "JJ" <a...@xyz.comwrote:
>>Yes - it will be an excel file they'll have. I guess 3000 lines is
possible
to cut and paste....
I'm not sure what the limitations are in terms of memory within excels
clipboard.

I would have though 3000 lines was well within limits, would you?
JJ

The Office clipboard is limited, but the Windows clipboard depends on
the RAM only.

I think, you should test if users will not have any problems with copy
and paste. I think 3000-lines-file is big, but not because of its size
in bytes but because you cannot be sure that you have pasted the whole
file in the small textbox area on the form. Though you can show the
number of submitted lines using a client js, and/ or on a postback


Jun 7 '07 #14
You can use ODBC and (I think) oledb to open files like this. It shouldn't
matter wether it is a comma or tab delimited as I think you can define that
when you open it.

(I have used ODBC in classic ASP to do it, but the method should be there in
..NET)

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"JJ" <ab*@xyz.comwrote in message
news:e1**************@TK2MSFTNGP02.phx.gbl...
What I mean is how would I temporarily hold the data whilst any
sorting/deleting/editing can take place?
"JJ" <ab*@xyz.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>What method should I use to sort/edit/delete the data, in case the user
wants to adjust the data before inputting it to the database?

JJ

"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@o11g2000prd.googleg roups.com...
>>On Jun 6, 9:33 pm, "JJ" <a...@xyz.comwrote:
Yes - it will be an excel file they'll have. I guess 3000 lines is
possible
to cut and paste....
I'm not sure what the limitations are in terms of memory within excels
clipboard.

I would have though 3000 lines was well within limits, would you?
JJ
The Office clipboard is limited, but the Windows clipboard depends on
the RAM only.

I think, you should test if users will not have any problems with copy
and paste. I think 3000-lines-file is big, but not because of its size
in bytes but because you cannot be sure that you have pasted the whole
file in the small textbox area on the form. Though you can show the
number of submitted lines using a client js, and/ or on a postback



Jun 7 '07 #15
JJ
I may well be wrong by saying this, but don't you have to have excel
installed on the server in order to treate the use the file as an excel
file?
(if that was what you were meaning).
Its a shared server on this occassion, so I cannot make sure any excel
components are on it.
(or have I completely got the wrong end of the stick....?)
JJ
"David" <da*****************@revilloc.REMOVETHIS.comwrot e in message
news:eM**************@TK2MSFTNGP06.phx.gbl...
You can use ODBC and (I think) oledb to open files like this. It shouldn't
matter wether it is a comma or tab delimited as I think you can define
that when you open it.

(I have used ODBC in classic ASP to do it, but the method should be there
in .NET)

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
"JJ" <ab*@xyz.comwrote in message
news:e1**************@TK2MSFTNGP02.phx.gbl...
>What I mean is how would I temporarily hold the data whilst any
sorting/deleting/editing can take place?
"JJ" <ab*@xyz.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>What method should I use to sort/edit/delete the data, in case the user
wants to adjust the data before inputting it to the database?

JJ

"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@o11g2000prd.google groups.com...
On Jun 6, 9:33 pm, "JJ" <a...@xyz.comwrote:
Yes - it will be an excel file they'll have. I guess 3000 lines is
possible
to cut and paste....
I'm not sure what the limitations are in terms of memory within excels
clipboard.
>
I would have though 3000 lines was well within limits, would you?
JJ
>

The Office clipboard is limited, but the Windows clipboard depends on
the RAM only.

I think, you should test if users will not have any problems with copy
and paste. I think 3000-lines-file is big, but not because of its size
in bytes but because you cannot be sure that you have pasted the whole
file in the small textbox area on the form. Though you can show the
number of submitted lines using a client js, and/ or on a postback



Jun 7 '07 #16
"JJ" <ab*@xyz.comwrote in message
news:ud**************@TK2MSFTNGP05.phx.gbl...
>I may well be wrong by saying this, but don't you have to have excel
installed on the server in order to treate the use the file as an excel
file?
No.

http://www.google.co.uk/search?sourc...2eNET%22+Excel
http://www.aspose.com/Products/Aspos...s/Default.aspx

If you're even considering installing Excel (or any other Office component)
on a webserver, then you need to start again...
--
http://www.markrae.net

Jun 7 '07 #17
JJ
Thanks Mark.

No I'm not considering installing any office component. I was merely
confused. I think its likely I'll stick to manipulating this input as a
plain text file. I just need to work out how to operate with it- i.e.
sort/page and allow the user to edit the fields _prior_ to inputting it into
the database.

I can do all that with objectdatasources fetching data from the database,
but not sure how I'd go about it in this case.
JJ
"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:OF*************@TK2MSFTNGP06.phx.gbl...
"JJ" <ab*@xyz.comwrote in message
news:ud**************@TK2MSFTNGP05.phx.gbl...
>>I may well be wrong by saying this, but don't you have to have excel
installed on the server in order to treate the use the file as an excel
file?

No.

http://www.google.co.uk/search?sourc...2eNET%22+Excel
http://www.aspose.com/Products/Aspos...s/Default.aspx

If you're even considering installing Excel (or any other Office
component) on a webserver, then you need to start again...
--
http://www.markrae.net

Jun 7 '07 #18
"JJ" <ab*@xyz.comwrote in message
news:u7**************@TK2MSFTNGP03.phx.gbl...
I can do all that with objectdatasources fetching data from the database,
but not sure how I'd go about it in this case.
Have you considered something like this:?
http://www.fpoint.com/netproducts/spreadweb/spread.aspx
--
http://www.markrae.net

Jun 7 '07 #19
JJ
This is a 'trial project' at the moment - so no budget. And as I'm merely a
poor, 'programmer' (meant in the loosest sense), I cannot afford such
things.

Thanks though, now I know what I'm missing....

JJ

"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
"JJ" <ab*@xyz.comwrote in message
news:u7**************@TK2MSFTNGP03.phx.gbl...
>I can do all that with objectdatasources fetching data from the database,
but not sure how I'd go about it in this case.

Have you considered something like this:?
http://www.fpoint.com/netproducts/spreadweb/spread.aspx
--
http://www.markrae.net

Jun 7 '07 #20
"JJ" <ab*@xyz.comwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
I cannot afford such things.
http://www.gemboxsoftware.com/GBSpreadsheetFree.htm
--
http://www.markrae.net

Jun 7 '07 #21
JJ
For a second there I thought you'd answered my prayers. Unfortunately I need
to test the trial system on a full size text file, so using the free
component with its limitations won't do the job.

Thanks though.

I'm actually there with the importing of the text - just not there with the
manipulation of it. Because it's so large, I'm at a loss as to how to keep
hold of the datatable so that I can sort/page it. Not sure whether things
like viewstate could hold such a large item...?
JJ
"Limitations
GemBox.Spreadsheet Free delivers the same performance and set of features as
the Professional version. However, the following limitations are imposed:

Maximum number of rows per sheet is 150.
Maximum number of sheets per workbook is 5.

The above limitations are enforced during writing or reading XLS or CSV
files."

"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
"JJ" <ab*@xyz.comwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>I cannot afford such things.

http://www.gemboxsoftware.com/GBSpreadsheetFree.htm
--
http://www.markrae.net

Jun 7 '07 #22
Here is how I do this in VBScript. You can do similar in .NET

It works, it will read txt and csv files just like a database. You can query
it.

set conn=CreateObject("adodb.connection")
conn.Open "DRIVER={Microsoft Text Driver (*.txt;
*.csv)};DefaultDir=/WorkingDir;"

set rs=CreateObject("adodb.recordset")
strSQL = "SELECT * FROM [MyFileName.csv] "
rs.Open strSQL, conn, 3, 3

if not rs.EOF then
do while not rs.EOF

'Do what you need to do with the data.

rs.movenext
loop
end if

'Close your connection and recordset
This is in VBScript. I have not done it yet it .NET though that will be
trivial. It will save all the effort of parsing the file to get what you
want. You can set up the ODBC parameters to however the data should be
parsed (delimited).

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available

"JJ" <ab*@xyz.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
For a second there I thought you'd answered my prayers. Unfortunately I
need to test the trial system on a full size text file, so using the free
component with its limitations won't do the job.

Thanks though.

I'm actually there with the importing of the text - just not there with
the manipulation of it. Because it's so large, I'm at a loss as to how to
keep hold of the datatable so that I can sort/page it. Not sure whether
things like viewstate could hold such a large item...?
JJ
"Limitations
GemBox.Spreadsheet Free delivers the same performance and set of features
as the Professional version. However, the following limitations are
imposed:

Maximum number of rows per sheet is 150.
Maximum number of sheets per workbook is 5.

The above limitations are enforced during writing or reading XLS or CSV
files."

"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>"JJ" <ab*@xyz.comwrote in message
news:%2***************@TK2MSFTNGP02.phx.gbl...
>>I cannot afford such things.

http://www.gemboxsoftware.com/GBSpreadsheetFree.htm
--
http://www.markrae.net


Jun 8 '07 #23

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

Similar topics

4
by: ralphNOSPAM | last post by:
Is there a function or otherwise some way to pull out the target text within an XML tag? For example, in the XML tag below, I want to pull out 'CALIFORNIA'. ...
3
by: Pir8 | last post by:
I have a complex xml file, which contains stories within a magazine. The structure of the xml file is as follows: <?xml version="1.0" encoding="ISO-8859-1" ?> <magazine> <story>...
26
by: SL33PY | last post by:
Hi, I'm having a problem parsing strings (comming from a flat text input file) to doubles. the code: currentImportDetail.Result = CType(line.Substring(7, 8).Trim(" "), System.Double) What...
1
by: Thomas Kowalski | last post by:
Hi, I have to parse a plain, ascii text file (on local HD). Since the file might be many millions lines long I want to improve the efficiency of my parsing process. The resulting data structure...
4
by: Neil.Smith | last post by:
I can't seem to find any references to this, but here goes: In there anyway to parse an html/aspx file within an asp.net application to gather a collection of controls in the file. For instance...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
2
by: hzgt9b | last post by:
I've written a simple javascript page that parses an XML file... (Actually I just modified the "Parsing an XML File" sample from http://www.w3schools.com/dom/dom_parser.asp) The page works great...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
1
by: martinsson | last post by:
Hi all! I'm pretty mad about this... dont know what is going on. Im parsing XML file that looks like this: <something> __<item att="something">text<item> __<item...
2
by: python | last post by:
I'm parsing a text file for a proprietary product that has the following 2 directives: #include <somefile> #define <name<value> Defined constants are referenced via <#name#syntax. I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.