read from comma delimited file

D

Danny

How can I read from a comma delimited text file and export to a tab
delimited file using perl.
the file has quotes around the fields when another comma is in the string,
but nothing if it is a regular string.
Like:
"this field has a comma, in it", but tihs field does not, good bye

I have searched for this but most do not account for the "quotes" around the
fields

THanks in advance
 
G

Gunnar Hjalmarsson

Danny said:
How can I read from a comma delimited text file and export to a tab
delimited file using perl.
the file has quotes around the fields when another comma is in the
string, but nothing if it is a regular string.
Like:
"this field has a comma, in it", but tihs field does not, good bye

I have searched for this but most do not account for the "quotes"
around the fields

If you really have searched for it, why are you asking a FAQ?

perldoc -q delimited
 
W

Walter Roberson

:How can I read from a comma delimited text file and export to a tab
:delimited file using perl.
:the file has quotes around the fields when another comma is in the string,
:but nothing if it is a regular string.
:Like:
:"this field has a comma, in it", but tihs field does not, good bye

What happens if the field has quotes in it? Quotes at the beginning and
end? With or without commas?

You haven't really fully specified the problem to us as yet.
 
J

John J. Trammell

How can I read from a comma delimited text file and export to a tab
delimited file using perl.

One solution would be to use the Text::CSV_XS module to read your
comma-delimited file.
 
D

Danny

Danny said:
How can I read from a comma delimited text file and export to a tab
delimited file using perl.
the file has quotes around the fields when another comma is in the string,
but nothing if it is a regular string.
Like:
"this field has a comma, in it", but tihs field does not, good bye

I have searched for this but most do not account for the "quotes" around the
fields

THanks in advance

I tried using the split() function but that really does not work
imagine cases like this:
"the quotes need to be here because of a , in the field"
so the split will not work for these fields

You can't remove the quotes before the split either

does anybody have a sample I can try?
 
G

Gunnar Hjalmarsson

Danny said:
I tried using the split() function but that really does not work
imagine cases like this:
"the quotes need to be here because of a , in the field"
so the split will not work for these fields

You can't remove the quotes before the split either

does anybody have a sample I can try?

You did get a few suggestions, but apparently you chose to ignore
them. Also, you haven't showed us any code that you have been trying.

Considering that, why would anybody be interested in posting code?
 
D

David K. Wall

Gunnar Hjalmarsson said:
If you really have searched for it, why are you asking a FAQ?

perldoc -q delimited

I think whoever wrote the FAQ entry has been using the Concurrent Versions
System too much. Text::CVS? Text::CVS_XS? :)

I'm still using 5.8.0 here at home, so possibly the FAQ has been corrected by
now.
 
S

Sherm Pendley

David said:
I think whoever wrote the FAQ entry has been using the Concurrent Versions
System too much. Text::CVS? Text::CVS_XS? :)

I'm still using 5.8.0 here at home, so possibly the FAQ has been corrected
by now.

Just looked at 5.8.4 - nope, it's not been corrected.

sherm--
 
S

Sherm Pendley

Danny said:
How can I read from a comma delimited text file and export to a tab
delimited file using perl.

That's most commonly called CSV, for "Comma Separated Values." Search for it
on CPAN - there are several modules for dealing with it.

There's also the secondary question of *why* you need to convert it to a tab
delimited file. Most apps that can import a tab delimited file - Excel,
various databases, etc. - can just as easily import a CSV file.

sherm--
 
J

Jim Cochrane

I tried using the split() function but that really does not work
imagine cases like this:
"the quotes need to be here because of a , in the field"
so the split will not work for these fields

You can't remove the quotes before the split either

does anybody have a sample I can try?

I bet it's possible to come up with an RE for the split that would work,
but it will not be an easy task.
 
D

David K. Wall

[re 'perldoc -q delimited']

Sherm Pendley said:
Just looked at 5.8.4 - nope, it's not been corrected.

I sent in a bug report, and mentioned this thread.
 
C

ctcgag

Jim Cochrane said:
I bet it's possible to come up with an RE for the split that would work,
but it will not be an easy task.


I'll quote myself from a few years ago:

==============
Alan Pettigrew said:
But what about embedded commas? What happens with a line like
field1,"field2a, field2b, field2c",field3 ...
I haven't worked out the regex for this. Perhaps one of you can.

This seems to work, but I'm sure someone will find some pathological cases
where it won't. (Other than the obvious of unpaired quotes). I don't
imagine that it is horribly efficient.

my @x = split /,(?=[^\"]*(?:\"[^\"]*\"[^\"]*)*$)/ ;


Xho
============

Note that this is specifically for the convention where literal q{"} are
protected by doubling them q{""}. After doing the split, you would still
have to strip the surrounding quotes off of fields that had surrounding
quotes, and undouble the q{""} from literal q{"}.

Xho
 
J

Jim Cochrane

It depends. Under certain circumstances you can do such things pretty
easily with a regex. This is an example of a similar (not identical)
problem:

http://groups.google.com/[email protected]

Yes, but it appears to be harder with split, because you can't use (...)
grouping without affecting the result of the split. I just learned about
this while playing around with another problem last night.
 
G

Glenn Jackman

Jim Cochrane said:
Yes, but it appears to be harder with split, because you can't use (...)
grouping without affecting the result of the split. I just learned about
this while playing around with another problem last night.

Sure you can. Just use non-capturing parentheses (?:my regex)
 
D

David K. Wall

I wrote:

[re mispelling in 'perldoc -q delimited']
I sent in a bug report, and mentioned this thread.

Mail to (e-mail address removed) has bounced on two separate attempts. Does
anyone know what is happening? Email to other places doesn't bounce,
so the problem doesn't seem to be on my end.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top