By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,421 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Parsing a text file

P: n/a
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
Share this Question
Share on Google+
22 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.