473,503 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem Parsing CSV File

2 New Member
Hi,

m using split function to parse the csv file ..... i m getting problem in one line of file which looks like this :

"BLUEUSBXM","X-MICRO (XBT-DG5R (C) R1) BLUETOOTH USB DONGLE PLUS, CLASS 1",360,0.00,"","",0.00,"",0,0.00,"","T1","",5.58," 4000",8.50,0.00,"02/10/2006",0.00,"16/04/2007"


now the problem is it parses first field properly.
BUT
Not parsing second field.....
avars = split(line,",")
should give:
avars(1) = "X-MICRO (XBT-DG5R (C) R1) BLUETOOTH USB DONGLE PLUS, CLASS 1"

instead of that it gives me:
avars(1) = "X-MICRO (XBT-DG5R (C) R1) BLUETOOTH USB DONGLE PLUS"

how can i solve this problem....

Thanks
Pindoriya1
Jul 3 '08 #1
12 4599
DrBunchman
979 Recognized Expert Contributor
The Split function is working correctly - it is splitting the file every time it finds a comma and there is a comma after the word PLUS.

To solve this you could make sure that all the fields that are entered into the CSV file have any comma's removed or you could parse the file in a different way.

Dr B
Jul 3 '08 #2
pindoriya1
2 New Member
The Split function is working correctly - it is splitting the file every time it finds a comma and there is a comma after the word PLUS.

To solve this you could make sure that all the fields that are entered into the CSV file have any comma's removed or you could parse the file in a different way.

Dr B


Thanks I know that that is why i want solution for that.....
actually the file comes from account software SO EITHER we change in the software OR we solve this problem once and for alll the time we get such feeds form anywhere.....

i prefer second way, as rather than telling accountign people to change name.... i want solution which i can use everywhere i get this situation................

YOU SUGGEST I COULD PARSE FILE IN DIFFERENT WAY....
so can you suggest me one or two of such ways....

thank you
pindoriya1
Jul 3 '08 #3
DrBunchman
979 Recognized Expert Contributor
The only thing I can think of that you might be able to do here is to loop through the string checking each character to see whether it's a comma. If it is a comma then check the next character to see whether it is a space - if so then it's going to be a comma inside one of your fields rather than one of your delimiters so you can ignore it.

It's not foolproof as somebody might enter some text which had a comma without a space after it but it will yield better results than the existing method.

Dr B
Jul 3 '08 #4
jhardman
3,406 Recognized Expert Specialist
best method: in the app that saves the CSV file, replace all commas with ",". You will also need to replace all instances in the existing csv file where there is a comma that isn't used as a delimiter with "," This will display correctly, can even be copied and pasted correctly, and the people who use it will never know the difference.

Second method: Try DrB's solution, but I would be happier if you added a trim() call to the original function that saves data to make sure no fields begin with a space, but this is still far from fool-proof. And if you are going to alter the app that saves the data, you might as well use the first solution.

Third method: Get a list of all products that have a comma, before you parse the .csv file, replace those texts with a version that doesn't contain commas. I would only recommend this if you have absolutely no access to the app that saves the csv file and you have a definite list of texts that have commas.

Let me know if this helps.

Jared
Jul 8 '08 #5
danp129
323 Recognized Expert Contributor
use regular expressions to parse CSV strings
Jul 9 '08 #6
jhardman
3,406 Recognized Expert Specialist
best method: in the app that saves the CSV file, replace all commas with ",". You will also need to replace all instances in the existing csv file where there is a comma that isn't used as a delimiter with "," This will display correctly, can even be copied and pasted correctly, and the people who use it will never know the difference.
and I spent all that time typing in the character code for a comma... I didn't notice that Bytes.com just replaced my code with commas. the code should be an ampersand followed by "#44;".

Jared
Jul 9 '08 #7
jhardman
3,406 Recognized Expert Specialist
use regular expressions to parse CSV strings
yes, I agree. Regex would be a good way to do the replaces.

Jared
Jul 9 '08 #8
DrBunchman
979 Recognized Expert Contributor
Of course, didn't think of that.

Good idea.
Jul 10 '08 #9
danp129
323 Recognized Expert Contributor
As an alternative to regexp's (due to the complexity of writing them) you could also use an ODBC connection with the Microsoft Text Driver to read the CSV file and select a specific record or recurse through all of them.

Provider=MSDASQL; Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=C:\path\;

In this case, the c:\path\ folder would be equivilant to a "database" and the filename would be the table. So if you have "c:\path\mydata.csv" then your select statement would be something like "Select * from mydata.csv".

There may be some limitations using this method, such as limited to 255 character length per field or maybe there is not a limit but the driver may only look at the first 10 rows to decide what the max length of a field is w/o editing the registry or creating an .ini file for the driver. You may also not be able to join "tables" (files) like a normal database, I haven't used csv files for quite some time so I can't say.

You can find some examples searching for: "Microsoft Text Driver" asp
Jul 10 '08 #10
jhardman
3,406 Recognized Expert Specialist
As an alternative to regexp's (due to the complexity of writing them) you could also use an ODBC connection with the Microsoft Text Driver to read the CSV file and select a specific record or recurse through all of them.
That's a good solution, but I don't think it would get around the problem of commas (which are reserved characters in csv files) being inserted by the user. Maybe one of us should test?

Jared
Jul 10 '08 #11
danp129
323 Recognized Expert Contributor
Commas in part of the text field are completely valid for CSV so long as a text qualifier (ie the double quotes) is used.
Jul 11 '08 #12
danp129
323 Recognized Expert Contributor
So if you have "c:\path\mydata.csv" then your select statement would be something like "Select * from mydata.csv".

Just noticed a typo, should be .... something like "Select * from mydata" w/o the .csv
Jul 11 '08 #13

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

Similar topics

0
1483
by: burn_hall | last post by:
Hi, I have a problem and can't figure it out and need your help, please look at the following code and the output also a xml file snippet is down there too. Looking at the output I don't know why...
2
3916
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
11
3547
by: Johnny Lee | last post by:
Hi, I was using urllib to grab urls from web. here is the work flow of my program: 1. Get base url and max number of urls from user 2. Call filter to validate the base url 3. Read the source...
10
2518
by: Bryce Calhoun | last post by:
Hello, First of all, this is a .NET 1.1 component I'm creating. SUMMARY ----------------------- This component that I'm creating is, for all intents and purposes, a document parser (I'm...
4
4843
by: Rick Walsh | last post by:
I have an HTML table in the following format: <table> <tr><td>Header 1</td><td>Header 2</td></tr> <tr><td>1</td><td>2</td></tr> <tr><td>3</td><td>4</td></tr> <tr><td>5</td><td>6</td></tr>...
1
3662
by: Martin Pöpping | last post by:
Hello, I´ve a problem with parsing a double value from an xml file. My code looks like this: int concept_id; double rank; XmlElement root = documentXMLString.DocumentElement; XmlNodeList...
27
5083
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set...
12
3381
by: Julian | last post by:
Hi, I am having problems with a function that I have been using in my program to read sentences from a 'command file' and parse them into commands. the surprising thing is that the program works...
7
2830
by: souravmallik | last post by:
Hello, I'm facing a big logical problem while writing a parser in VC++ using C. I have to parse a file in a chunk of bytes in a round robin fashion. Means, when I select a file, the parser...
0
7083
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
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7456
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.