Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 21st, 2004, 09:45 AM
mel_bagley mel_bagley is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 9 mel_bagley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry Comma delimeted file - problems - Help

Hello,

I'm trying to inport in a csv file to a vb array, and trying to split the following:

1,CYH7877,Happy,"my house, my home",0928283,RED

what i want to do is split the fields (seperated by , ) but the field in which a comma occurs inside quotes; MUST NOT be split, all the info inside the quotes is pulled into a field and left in quotes.

e.g.

row 0,3 = "my house, my home"
NOT
row 0,3 = my house
row 0,4 = my home

So the program just skips over the quotes and takes the whole quoted text as a field.

Sorry if this was not made clearer in my previous postings.

kind regards

p.s. Thanks fisherman for all your help so far.

Reply With Quote
  #2  
Old January 21st, 2004, 10:51 AM
victorpendleton victorpendleton is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: No es importante
Posts: 2,065 victorpendleton User rank is Private First Class (20 - 50 Reputation Level)victorpendleton User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 50 m 52 sec
Reputation Power: 8
One solution: When the application encounters a double-quote take all the information until it hits a closing double quote. Create some sort of exception clause to handle this in your parsing routine.
__________________
El éxito consiste en una serie de pequeñas victorias día a día

MySQL, MS SQL, MS ACCESS, Oracle Database Manager - http://victorpendleton.net/products/psdviewer.html

Reply With Quote
  #3  
Old January 21st, 2004, 12:25 PM
Fisherman's Avatar
Fisherman Fisherman is offline
Inherits Programmer.Slacker
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Between my Id and your Ego
Posts: 2,194 Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 13 h 57 m 53 sec
Reputation Power: 142
Send a message via ICQ to Fisherman Send a message via AIM to Fisherman
ok... taking your sample, I made this.
Code:
Private Sub Form_Load()
Dim str1 As String
Dim strArr() As String
Dim i As Integer
Dim j As Integer
str1 = "1,CYH7877,Happy,""my house, my home"",0928283,RED"

MsgBox str1
strArr = Split(str1, ",", , vbTextCompare)
For i = 0 To UBound(strArr) - 1
    If InStr(1, strArr(i), Chr(34), vbTextCompare) Then
        strArr(i) = strArr(i) & strArr(i + 1)
        For j = (i + 1) To UBound(strArr) - 1
            strArr(j) = strArr(j + 1)
        Next j
        ReDim Preserve strArr(UBound(strArr) - 1)
    End If
Next i
For i = 0 To UBound(strArr)
    MsgBox strArr(i)
Next i
End Sub


Last edited by Fisherman : January 21st, 2004 at 12:29 PM.

Reply With Quote
  #4  
Old January 21st, 2004, 05:27 PM
lueg66 lueg66 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 13 lueg66 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 40 m 27 sec
Reputation Power: 0
Hi,

We had a similar problem with ‘ (single quotes) “O’Brians”
and creating SQL strings.

If you create the csv yourself this approach may work:

1. Before creating the csv replace all , with something like ^^
2. Create your csv file it should look like below
1,CYH7877,Happy,my house^^ my home,0928283,RED
3. Load it into your array
4. In your array replace all ^^ with ,

Regards,

Edi

Reply With Quote
  #5  
Old January 21st, 2004, 05:57 PM
Fisherman's Avatar
Fisherman Fisherman is offline
Inherits Programmer.Slacker
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Between my Id and your Ego
Posts: 2,194 Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 13 h 57 m 53 sec
Reputation Power: 142
Send a message via ICQ to Fisherman Send a message via AIM to Fisherman
good idea. Didn't think about that. Thanks Lueg!

Reply With Quote
  #6  
Old January 21st, 2004, 05:58 PM
Fisherman's Avatar
Fisherman Fisherman is offline
Inherits Programmer.Slacker
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Between my Id and your Ego
Posts: 2,194 Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 13 h 57 m 53 sec
Reputation Power: 142
Send a message via ICQ to Fisherman Send a message via AIM to Fisherman
victor - wouldn't you need to read the string character by character for that?

Reply With Quote
  #7  
Old January 22nd, 2004, 01:26 AM
victorpendleton victorpendleton is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: No es importante
Posts: 2,065 victorpendleton User rank is Private First Class (20 - 50 Reputation Level)victorpendleton User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 50 m 52 sec
Reputation Power: 8
Fisherman, you are correct. That would force a character by character iteration. Not the most elegant method, and definitely not for a huge flat file, but would allow the user a granular customization. If the file was large I would suggest taking the time to import the file into some sort of database and have Visual Basic read that instead of a flat file.

Reply With Quote
  #8  
Old January 24th, 2004, 01:02 AM
Fisherman's Avatar
Fisherman Fisherman is offline
Inherits Programmer.Slacker
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Between my Id and your Ego
Posts: 2,194 Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 13 h 57 m 53 sec
Reputation Power: 142
Send a message via ICQ to Fisherman Send a message via AIM to Fisherman
mel- I'm just curious. Did you resolve your problem?

Reply With Quote
  #9  
Old January 28th, 2004, 05:04 AM
mel_bagley mel_bagley is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 9 mel_bagley User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi Fisherman, I used a tempory measure, of changing all the commas to ^ prior to using my program, i will keep a look out for a proper solution.

Thanks for your help..

Reply With Quote
  #10  
Old January 30th, 2004, 01:18 PM
middenaveen middenaveen is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Bowling Green, Kentucky
Posts: 22 middenaveen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile hi, i have a solution, try this

hi,

you can get that done by just usinng the following code,

As you are using the .csv file you can split this line

1,CYH7877,Happy,"my house, my home",0928283,RED

like using the following code:


open "c:\temp.csv" for input as #1

do while not eof(1)

input #1, row, str1, str2, str3, str4,str5,str6,str7


loop



Please let me know wht the result is::

All The Best,

bye

Reply With Quote
  #11  
Old January 30th, 2004, 01:50 PM
Fisherman's Avatar
Fisherman Fisherman is offline
Inherits Programmer.Slacker
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Aug 2003
Location: Between my Id and your Ego
Posts: 2,194 Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level)Fisherman User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 13 h 57 m 53 sec
Reputation Power: 142
Send a message via ICQ to Fisherman Send a message via AIM to Fisherman
mid - wouldn't that still split the string inside the quotes?

Reply With Quote
  #12  
Old January 31st, 2004, 02:09 AM
middenaveen middenaveen is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Bowling Green, Kentucky
Posts: 22 middenaveen User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hi, I am sorry its not working

hi,

i am sorry for that, but my code works if the same file has the data stored completely in the double codes like,

"1","xxx","dddd"

if the data is in that format, it works well.

ok bye

Reply With Quote
  #13  
Old February 2nd, 2004, 12:01 AM
eijkhof eijkhof is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: The Netherlands
Posts: 2 eijkhof User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hello,

is somebody in here willing and able to solve my problem as well? I'd like to read info from a csv-file, but the format of the first entry is strange. What I do:

Set csvfile = fso.OpenTextFile(strFilePath & strFileName, 1)
Dim dummy_array() As String
sline = csvfile.ReadLine
dummy_array() = Split(sline, ",")

Now the data looks like:
csv-line: NUMBER,HNUM, ... ,TERMTI

and the variables like:
sline: : ""NUMBER,HNUM, ... ,TERMTI"
CStr(dummy_array(0)): ""NUMBER"

... while I want the latter to look like "Number" (note the number of *"*). Where does this strange starting " come from, and how to get rid of it??? The moment I want to read numbers, the resulting ""1" suddenly is not recognized as numeric anymore (while it should be ).

I appreciate any comment!
Frank

Last edited by eijkhof : February 2nd, 2004 at 12:12 AM.

Reply With Quote
  #14  
Old February 5th, 2004, 08:20 PM
larryjoe larryjoe is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 6 larryjoe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
this might give you a "kickstart"

found this code on the net:
Option Explicit

'
'
'
'
' This shows how to parse strings (separate line of text into words)

Private Sub Command1_Click()
Dim linoftext$, i As Integer, delimiter$, k As Integer, B$
delimiter$ = Text2.Text
linoftext$ = Text1.Text
' First, make all the separators the same
For i = 2 To Len(delimiter$)
k = InStr(linoftext$, Mid$(delimiter$, i, 1))
Do While k
Mid$(linoftext$, k, 1) = Left$(delimiter$, 1)
k = InStr(linoftext$, Mid$(delimiter$, i, 1))
Loop
Next
' Now parse it!
Do While Len(linoftext$)
k = InStr(linoftext$, Left$(delimiter$, 1))
If k = 1 Then
linoftext$ = Mid$(linoftext$, 2)
ElseIf k Then
B$ = Left$(linoftext$, k - 1)
' Debug.Print B$
List1.AddItem B$
linoftext$ = Mid$(linoftext$, k + 1)
Else
List1.AddItem linoftext$
linoftext$ = ""
End If
Loop
End Sub

Reply With Quote
  #15  
Old February 6th, 2004, 04:04 AM
eijkhof eijkhof is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Location: The Netherlands
Posts: 2 eijkhof User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi Larryjoe,

thank you for your reply. It answers one of my quesions, but not the problem described above - that one I solved yesterday (I just didn't post about it, sorry!). It appears that the format of the input file was at fault - the first two lines were actually formatted as 'strings of strings', whereas the remainder were just strings. I could not see this because my excell suppressed the format. A simple editing action in an ascii-editor solved the whole problem rather easily.

Thank you for you suggestion!
Frank

Reply With Quote