473,386 Members | 1,745 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,386 software developers and data experts.

[ASP] Find and Parse a specific line in CSV?

80
Hi All!

Could anybody provide me with the fastest and best way to search for a specific value in a (VERY LARGE) CSV file and parse that exact line?

The CSV file has about 90.000 to 120.000 Lines and are build like this:

Expand|Select|Wrap|Line Numbers
  1. "84016","Bags","Belkin iPod Nano Remix Acrylcase black","19","Yes","0.1","F8Z381EA"
I would like to be able to finde "84016" and from there on parse the other values from that line.

Regards and thanks.
Frederik
Aug 17 '10 #1

✓ answered by azrar

Try something like this:

Expand|Select|Wrap|Line Numbers
  1. start_pos = Instr(1, csv_data, vbCrLf & Chr(34) & "84016")
  2. If start_pos > 0 Then
  3.  '-- find the end of the line
  4.  end_pos = Instr(start_pos + 2, csv_data, vbCrLf)
  5.  
  6.  '-- extract that one line based on start and end positions
  7.  onerow = Mid(csv_data, start_pos, end_pos - start_pos)
  8. Else
  9.  '-- the searched data does not exist
  10. End IF
  11.  
The trick is to include the line break in your instr search, so you know you are at the beginning of the line.

Note: for best results, make sure there is a line break at the very beginning and the very end of the csv data.

6 2018
azrar
11
Try something like this:

Expand|Select|Wrap|Line Numbers
  1. start_pos = Instr(1, csv_data, vbCrLf & Chr(34) & "84016")
  2. If start_pos > 0 Then
  3.  '-- find the end of the line
  4.  end_pos = Instr(start_pos + 2, csv_data, vbCrLf)
  5.  
  6.  '-- extract that one line based on start and end positions
  7.  onerow = Mid(csv_data, start_pos, end_pos - start_pos)
  8. Else
  9.  '-- the searched data does not exist
  10. End IF
  11.  
The trick is to include the line break in your instr search, so you know you are at the beginning of the line.

Note: for best results, make sure there is a line break at the very beginning and the very end of the csv data.
Aug 17 '10 #2
Pheddy
80
Thank you azrar!

The code works fine. But now I have one line and would like to extract elements from this, can you help me with that?
Aug 19 '10 #3
azrar
11
Given the line:
"84016","Bags","Belkin iPod Nano Remix Acrylcase black","19","Yes","0.1","F8Z381EA"

You can make array out of the data like this:
Expand|Select|Wrap|Line Numbers
  1. myArray = Split(csv_data,Chr(34) & "," & Chr(34))
The first and last element will have an extra quote in it. You can avoid that by tweaking the code I gave you earlier (add 1 to the start position, subtract 1 from the end position).
Aug 19 '10 #4
jhardman
3,406 Expert 2GB
I disagree, I would use a db engine and query the csv
Expand|Select|Wrap|Line Numbers
  1. "select * from file where field1 = 84016"
The microsoft JET ODBC driver has that capability.

On the other hand, with 120 thousand rows, why haven't you converted to a db yet?

Jared
Aug 25 '10 #5
Pheddy
80
Thank you very much for that reply, I am trying to parse this file down to put the fields into a database. So i am looking for the exact thing you asked of..

I would be very thankfull if you could guide me further.

Regards
Frederik
Aug 25 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: hue | last post by:
I am trying to parse command line options using getopt module. I have written the following Code import string import getopt def usage(): print '''haarp_make.py -- uses getopt to...
3
by: supster | last post by:
I'de like to change only a specific line of a textbox. Right now I am doing: string texta = mainTextBox.Text.Split( "\n".ToCharArray() ); for ( int i = 0; i <= texta.Length-1; i++ ) {
11
by: Xero | last post by:
Hello. How do you erase a specific line in a *.txt file? Thanks in advance. -- Xero http://www.chezjeff.net My personal web portal
5
by: kevin arana | last post by:
Hello. Could anyone please show me an example of how to retrieve a specific line of a text file (say line 3 of data.txt) into a string variable? I have seen many examples on how to dump all...
0
by: frankleggett | last post by:
Hi Excuse my ignorance but I am trying to write a very simple program. It needs to be simple because of my lack of knowledge. Anyway I would like to input a specific line from a textfile without...
2
by: uche | last post by:
Hello all!! Is there an algorithm out there that will allow you to go to a specific line in a .txt...and start outputing to the screen at that line until end of record Here is a code that i...
0
by: Rajgodfather | last post by:
I am getting the end couldn't error while validating xml file against xsd. The xml file looks perfect. XML: <event id="1"> <!-- Successful event. --> ...
11
by: jo3c | last post by:
hi everybody im a newbie in python i need to read line 4 from a header file using linecache will crash my computer due to memory loading, because i am working on 2000 files each is 8mb ...
5
by: Patrick David | last post by:
Hello NG, I am searching for a way to jump to a specific line in a text file, let's say to line no. 9000. Is there any method like file.seek() which leads me to a given line instead of a given...
6
by: Ramesh | last post by:
Hello, I am using the ofstream class to create a text file with keys and values like: Key1=Value10 Key2=Value15 Key3=Value20 In case I need to set a new value for Key2, say value50 - I am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.