Parsing a text file

J

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
 
A

Alexey Smirnov

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();
 
A

Alexey Smirnov

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);
 
J

JJ

Great - thanks.
JJ
Alexey Smirnov said:
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();
 
J

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 said:
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);
 
A

Alexey Smirnov

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
 
J

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
 
J

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
 
A

Alexey Smirnov

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...
 
J

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
 
A

Alexey Smirnov

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
 
J

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
 
J

JJ

What I mean is how would I temporarily hold the data whilst any
sorting/deleting/editing can take place?
 
D

David

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
 
J

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
 
J

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
 
J

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top