Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

Reading CSV File Using ASP

Thread Tools
 
Search this Thread
 
Old 02 August 2005, 03:19 PM
  #1  
ScoobyDan
Scooby Regular
Thread Starter
 
ScoobyDan's Avatar
 
Join Date: Apr 2001
Location: West Sussex
Posts: 227
Likes: 0
Received 0 Likes on 0 Posts
Question 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:
Code:
0001,blah,blah,blah
0002,blah,blah,blah
0003,blah,blah,blah
...
And the URL would look like:
Code:
default.asp?id=0002
(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
Old 02 August 2005, 03:26 PM
  #2  
stevencotton
Scooby Regular
 
stevencotton's Avatar
 
Join Date: Jan 2001
Location: behind twin turbos
Posts: 2,710
Likes: 0
Received 1 Like on 1 Post
Default

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.
Old 02 August 2005, 03:58 PM
  #3  
ScoobyDan
Scooby Regular
Thread Starter
 
ScoobyDan's Avatar
 
Join Date: Apr 2001
Location: West Sussex
Posts: 227
Likes: 0
Received 0 Likes on 0 Posts
Default

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
Old 03 August 2005, 09:00 AM
  #4  
NWMark
Scooby Regular
 
NWMark's Avatar
 
Join Date: Oct 2003
Location: Cheshire
Posts: 671
Likes: 0
Received 0 Likes on 0 Posts
Default

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

Last edited by NWMark; 03 August 2005 at 09:02 AM.
Old 03 August 2005, 11:42 AM
  #5  
David_Wallis
Scooby Regular
 
David_Wallis's Avatar
 
Join Date: Nov 2001
Location: Leeds - It was 562.4bhp@28psi on Optimax, How much closer to 600 with race fuel and a bigger turbo?
Posts: 15,239
Likes: 0
Received 1 Like on 1 Post
Default

what code you using to read the file?

Might be easier to write an exe to read the info?
Old 03 August 2005, 12:17 PM
  #6  
SJ_Skyline
Scooby Senior
 
SJ_Skyline's Avatar
 
Join Date: Apr 2002
Location: Limbo
Posts: 21,922
Likes: 0
Received 1 Like on 1 Post
Default

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
Old 03 August 2005, 12:22 PM
  #7  
SJ_Skyline
Scooby Senior
 
SJ_Skyline's Avatar
 
Join Date: Apr 2002
Location: Limbo
Posts: 21,922
Likes: 0
Received 1 Like on 1 Post
Default

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)
Old 03 August 2005, 01:04 PM
  #8  
ScoobyDan
Scooby Regular
Thread Starter
 
ScoobyDan's Avatar
 
Join Date: Apr 2001
Location: West Sussex
Posts: 227
Likes: 0
Received 0 Likes on 0 Posts
Default

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
%>
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Darrell@Scoobyworx
Trader Announcements
26
30 January 2024 01:27 PM
Sam Witwicky
Engine Management and ECU Remapping
17
13 November 2015 10:49 AM
crazyspeedfreakz
ScoobyNet General
5
29 September 2015 05:04 PM
Nick_Cat
Computer & Technology Related
2
26 September 2015 08:00 AM
Littleted
Computer & Technology Related
0
25 September 2015 08:44 AM



Quick Reply: Reading CSV File Using ASP



All times are GMT +1. The time now is 08:50 PM.