Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
CSV files with comma and quotes
Message
From
09/11/2018 09:24:20
 
 
To
09/11/2018 08:46:59
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
Miscellaneous
Thread ID:
01663238
Message ID:
01663245
Views:
66
This message has been marked as a message which has helped to the initial question of the thread.
>Hi All
>
>I have a CSV file which uses comma as the primary delimiter between fields. However, some fields might contain a comma (e.g. a description field) and the supplier provides those fields enclosed in quotes. For example:
>
>AAA,BBB,"XXX YYY, ZZZ",CCC,"X1, Y1, Z1",DDD,
>
>I cannot do an import on this CSV file as it contains too many fields. I need to parse the file, line by line.
>
>How can I get each field so that the result would be:
>
>Field 1: AAA
>Field 2: BBB
>Field 3: XXX YYY, ZZZ
>Field 4: CCC
>Field 5: X1, Y1, Z1
>Field 6: DDD
>
>GETWORDNUM() works for fields separated with a comma but then it picks up individual sections in the description fields which are enclosed in quotes. I can write a manual parsing routine but was wondering if there was a faster way.

The only reliable way I have found is to

(1) copy the line, then
(2) replace the double quotes and everything between them with eg ~ (by pairs of " )
(3) find the position of every comma ( use a regex )
(4) split the original line using the positions of every comma of (3)

>
>TIA
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform