CSV problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Grove

    CSV problems

    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:
    Affiliate marketing is a simple way to earn money online, using our affiliate platform. Join a global community of publishers and advertisers.

    Affiliate marketing is a simple way to earn money online, using our affiliate platform. Join a global community of publishers and advertisers.






    ---
    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


  • Rob Allen

    #2
    Re: CSV problems

    In message <3fb8e3c9$0$108 $65c69314@mercu ry.nildram.net> , Richard Grove
    <info@[at].invalid> writes[color=blue]
    >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.
    >[/color]

    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

    Comment

    • Michael Fuhr

      #3
      Re: CSV problems

      "Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:
      [color=blue]
      > 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.[/color]

      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.
      [color=blue]
      > I am wondering of it is possible to replace these commas in the fields
      > without replacing the CSV structure.[/color]

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

      --
      Michael Fuhr

      Comment

      • Richard Grove

        #4
        Re: CSV problems

        "Michael Fuhr" <mfuhr@fuhr.org > wrote in message
        news:3fb8f879$1 _1@omega.dimens ional.com...[color=blue]
        > "Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:
        >[color=green]
        > > 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,[/color][/color]
        the[color=blue][color=green]
        > > whole thing bugs out.[/color]
        >
        > 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.
        >[color=green]
        > > I am wondering of it is possible to replace these commas in the fields
        > > without replacing the CSV structure.[/color]
        >
        > Let's get all the facts before looking for a solution.
        >
        > --
        > Michael Fuhr
        > http://www.fuhr.org/~mfuhr/[/color]



        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:
        Affiliate marketing is a simple way to earn money online, using our affiliate platform. Join a global community of publishers and advertisers.

        Affiliate marketing is a simple way to earn money online, using our affiliate platform. Join a global community of publishers and advertisers.






        ---
        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


        Comment

        • Michael Fuhr

          #5
          Re: CSV problems

          "Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:
          [color=blue]
          > "Michael Fuhr" <mfuhr@fuhr.org > wrote in message
          > news:3fb8f879$1 _1@omega.dimens ional.com...[color=green]
          > > "Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:
          > >[color=darkred]
          > > > 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,[/color][/color]
          > the[color=green][color=darkred]
          > > > whole thing bugs out.[/color]
          > >
          > > 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.[/color]
          >
          > My CSV file doesn't have quotes.[/color]

          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.
          [color=blue]
          > 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.[/color]

          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

          Comment

          • Matthew Crouch

            #6
            Re: CSV problems

            "Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> wrote in message
            news:3fb8fe02$0 $119$65c69314@m ercury.nildram. net...[color=blue]
            > "Michael Fuhr" <mfuhr@fuhr.org > wrote in message
            > news:3fb8f879$1 _1@omega.dimens ional.com...[color=green]
            > > "Richard Grove" <info[at]redeyemedia[dot]co[dot]uk> writes:
            > >[color=darkred]
            > > > 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,[/color][/color]
            > the[color=green][color=darkred]
            > > > whole thing bugs out.[/color]
            > >
            > > 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.
            > >[color=darkred]
            > > > I am wondering of it is possible to replace these commas in the fields
            > > > without replacing the CSV structure.[/color]
            > >
            > > Let's get all the facts before looking for a solution.
            > >
            > > --
            > > Michael Fuhr
            > > http://www.fuhr.org/~mfuhr/[/color]
            >
            >
            >
            > 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.
            >[/color]

            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.


            Comment

            Working...