Reading CSV File Using ASP
#1
Scooby Regular
Thread Starter
Join Date: Apr 2001
Location: West Sussex
Posts: 227
Likes: 0
Received 0 Likes
on
0 Posts
Reading CSV File Using ASP
Hi All,
I want to read a 4,000 row CSV file, and display the data in one of the rows (identified via a field that is included in the URL). The file looks something like this:
And the URL would look like:
(for the second row)
Currently, the page reads the whole of the CSV file (which takes a while!), creating an array for each row. I then identify the row with the correct id, and display the data.
Now for my question (I get there eventually!):
How can I get my ASP page to read just the row I want, rather than all 4,000 rows?
Thanks in advance
Daniel
I want to read a 4,000 row CSV file, and display the data in one of the rows (identified via a field that is included in the URL). The file looks something like this:
Code:
0001,blah,blah,blah 0002,blah,blah,blah 0003,blah,blah,blah ...
Code:
default.asp?id=0002
Currently, the page reads the whole of the CSV file (which takes a while!), creating an array for each row. I then identify the row with the correct id, and display the data.
Now for my question (I get there eventually!):
How can I get my ASP page to read just the row I want, rather than all 4,000 rows?
Thanks in advance
Daniel
#2
Scooby Regular
You can't, you'll always have to read in every row up to the one you're after, assuming you want to stop when you've found a match. Will your CSV grow? It will be more and more inefficient the longer it gets, you need to put the data in a database.
#3
Scooby Regular
Thread Starter
Join Date: Apr 2001
Location: West Sussex
Posts: 227
Likes: 0
Received 0 Likes
on
0 Posts
Thanks Steve.
The CSV file will stay a constant size, and I am unable to change the format.
I will test out just checking the first 4 characters of each row until the required row is found (hadn't thought of that ).
Daniel
The CSV file will stay a constant size, and I am unable to change the format.
I will test out just checking the first 4 characters of each row until the required row is found (hadn't thought of that ).
Daniel
#4
Scooby Regular
Join Date: Oct 2003
Location: Cheshire
Posts: 671
Likes: 0
Received 0 Likes
on
0 Posts
Instead of reading the file in a line at a time using a loop, it will probably be much quicker to read the whole csv file in one go and then search it once the whole file is in memory.
will use loads of memory, so if its on a busy server or needs to be carried out very often, probably not the best way to do it.
Mark
will use loads of memory, so if its on a busy server or needs to be carried out very often, probably not the best way to do it.
Mark
Last edited by NWMark; 03 August 2005 at 09:02 AM.
#6
Loop through the CSV using the skipline method and then call the readline method on your FSO. It's probably more efficient than reading all the lines and a damn sight more efficient than reading them into an array.
MSDN
The problem is FSO is just about the slowest way of reading data bar typing it in yourself
MSDN
The problem is FSO is just about the slowest way of reading data bar typing it in yourself
#7
Something like this - not tested!!
Code:
Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("c:\testfile.txt", ForReading) for intCounter = 1 to 500 f.SkipLine next response.write(f.ReadLine)
Trending Topics
#8
Scooby Regular
Thread Starter
Join Date: Apr 2001
Location: West Sussex
Posts: 227
Likes: 0
Received 0 Likes
on
0 Posts
Thanks All for your suggestions. This is the script I am now using, which is a lot quicker than reading the whole file into an array!
Code:
<% csv_to_read = "test.csv" set fso = createobject("scripting.filesystemobject") set act = fso.opentextfile(server.mappath(csv_to_read)) id = request("id") Do While Not act.AtEndOfStream imported_text = act.readline 'Read the next line of the csv id_text = left(imported_text,4) 'Identify the ID field if (id_text = id) then imported_text = replace(imported_text,chr(13),",") 'Change the line breaks to commas to delimit through-out imported_text = replace(imported_text,chr(34),"") 'Remove all quotes split_text=split(imported_text,",") 'Split the line by comma num_imported=ubound(split_text)+1 'Count the number of splits and add one for the last element end if Loop act.Close set act = Nothing set fso = Nothing %>
Thread
Thread Starter
Forum
Replies
Last Post
Sam Witwicky
Engine Management and ECU Remapping
17
13 November 2015 10:49 AM