By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,641 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,641 IT Pros & Developers. It's quick & easy.

CSV problems

P: n/a
Here's one for you.
I import a CSV file which is then read using fgetcsv();
My problem is that if the fields in the CSV file have commas in them, the
whole thing bugs out.
I am wondering of it is possible to replace these commas in the fields
without replacing the CSV structure.

Any help greatly appreciated.

Regards
Richard Grove
http://shopbuilder.org - ecommerce systems
Become a Shop Builder re-seller:
http://www.affiliatewindow.com/affil...ls.php?mid=611
http://www.affiliatewindow.com/a.pl?590

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.536 / Virus Database: 331 - Release Date: 03/11/2003
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In message <3f*********************@mercury.nildram.net>, Richard Grove
<info@[at].invalid> writes
Here's one for you.
I import a CSV file which is then read using fgetcsv();
My problem is that if the fields in the CSV file have commas in them, the
whole thing bugs out.
I am wondering of it is possible to replace these commas in the fields
without replacing the CSV structure.


In general a CSV format file should have double-quote marks around the
field data if it has commas in it.

E.g.

1,2,3,some test data,5
1,2,3,"some, test data",5

fgetcsv() should then read this correctly.

--
Rob Allen
Jul 17 '05 #2

P: n/a
"Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:
I import a CSV file which is then read using fgetcsv();
My problem is that if the fields in the CSV file have commas in them, the
whole thing bugs out.
What exactly happens when "the whole thing bugs out"? Are the
commas in the fields inside quotation marks? Are there quotation
marks in the file that could be confusing fgetcsv()?

Could you post a line from the CSV file and the code you're using
to read it? That way we could see exactly what's happening.
I am wondering of it is possible to replace these commas in the fields
without replacing the CSV structure.


Let's get all the facts before looking for a solution.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 17 '05 #3

P: n/a
"Michael Fuhr" <mf***@fuhr.org> wrote in message
news:3f**********@omega.dimensional.com...
"Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:
I import a CSV file which is then read using fgetcsv();
My problem is that if the fields in the CSV file have commas in them, the whole thing bugs out.


What exactly happens when "the whole thing bugs out"? Are the
commas in the fields inside quotation marks? Are there quotation
marks in the file that could be confusing fgetcsv()?

Could you post a line from the CSV file and the code you're using
to read it? That way we could see exactly what's happening.
I am wondering of it is possible to replace these commas in the fields
without replacing the CSV structure.


Let's get all the facts before looking for a solution.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/


My CSV file doesn't have quotes.
When it bugs out, what I meant to say was that when it populates the
databse, it skips columns and puts values in the wrong place.
Not really bugging out I suppose.

Regards
Richard Grove
01892 546979
http://shopbuilder.org - ecommerce systems
Become a Shop Builder re-seller:
http://www.affiliatewindow.com/affil...ls.php?mid=611
http://www.affiliatewindow.com/a.pl?590

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.536 / Virus Database: 331 - Release Date: 03/11/2003
Jul 17 '05 #4

P: n/a
"Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:
"Michael Fuhr" <mf***@fuhr.org> wrote in message
news:3f**********@omega.dimensional.com...
"Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:
I import a CSV file which is then read using fgetcsv();
My problem is that if the fields in the CSV file have commas in them, the whole thing bugs out.
What exactly happens when "the whole thing bugs out"? Are the
commas in the fields inside quotation marks? Are there quotation
marks in the file that could be confusing fgetcsv()?

Could you post a line from the CSV file and the code you're using
to read it? That way we could see exactly what's happening.


My CSV file doesn't have quotes.


If a comma is the field separater, and if a field's value contains
commas, then that field needs to be quoted. Without some way to
distinguish which commas are part of a field and which commas
separate different fields, you'll get unexpected results.
When it bugs out, what I meant to say was that when it populates the
databse, it skips columns and puts values in the wrong place.
Not really bugging out I suppose.


Make sure you're reading the data correctly before worrying about
how it's inserted into a database.

As I asked before, please post a troublesome line from the CSV file
and the code you're using to read it. If we can see what you're
doing then we might be able to offer advice on how to fix the
problem.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 17 '05 #5

P: n/a
"Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> wrote in message
news:3f*********************@mercury.nildram.net.. .
"Michael Fuhr" <mf***@fuhr.org> wrote in message
news:3f**********@omega.dimensional.com...
"Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:
I import a CSV file which is then read using fgetcsv();
My problem is that if the fields in the CSV file have commas in them, the whole thing bugs out.


What exactly happens when "the whole thing bugs out"? Are the
commas in the fields inside quotation marks? Are there quotation
marks in the file that could be confusing fgetcsv()?

Could you post a line from the CSV file and the code you're using
to read it? That way we could see exactly what's happening.
I am wondering of it is possible to replace these commas in the fields
without replacing the CSV structure.


Let's get all the facts before looking for a solution.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/


My CSV file doesn't have quotes.
When it bugs out, what I meant to say was that when it populates the
databse, it skips columns and puts values in the wrong place.
Not really bugging out I suppose.


Sounds like the csv is not being properly generated. Without those quotes,
there's really no way to know where you're trying to separate the fields.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.