How to parse CSV file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Afonin

    How to parse CSV file

    Hello,

    I need to parse a string that returns the domain DNS records and to put this
    data into a DataTable. I don't have much experience in parsing strings, so
    I'm not aware of the efficient way how to do this. I've found some examples
    of CSV parser classes, but not sure how to use them in my case.

    Here is an example of the string:

    RRs\nsubname: @;priority: 5;address: mail.domainname .net.;rectype:
    MX;\nsubname: dns;priority: 1;address: ns1.domainhost. net.;rectype:
    NS;\nsubname: dns;priority: 2;address: ns2.domainhost. net.;rectype:
    NS;\nsubname: help;priority: 0;address: 67.22.36.4;rect ype: A;\nsubname:
    mail;priority: 0;address: domainname.net. ;rectype: CNAME;\nsubname :
    support;priorit y: 0;address: help.domainhost .net.;rectype: CNAME;\nsubname :
    text;priority: 0;address: \"tex value\";rectype : TXT;

    Each record is separated by "\n". The columns are:

    subname
    rectype
    priority
    address

    I would appreciate very much if you could point me to the right direction
    how to parse this string efficiently.

    Thank you,

    Peter

  • Cor Ligthert[MVP]

    #2
    Re: How to parse CSV file

    Peter,

    Use OleDB for this about 1000.000 samples on Internet how to do this.

    Cor

    "Peter Afonin" <petera@gudzon. netschreef in bericht
    news:%23Ok9cIVu IHA.4476@TK2MSF TNGP06.phx.gbl. ..
    Hello,
    >
    I need to parse a string that returns the domain DNS records and to put
    this data into a DataTable. I don't have much experience in parsing
    strings, so I'm not aware of the efficient way how to do this. I've found
    some examples of CSV parser classes, but not sure how to use them in my
    case.
    >
    Here is an example of the string:
    >
    RRs\nsubname: @;priority: 5;address: mail.domainname .net.;rectype:
    MX;\nsubname: dns;priority: 1;address: ns1.domainhost. net.;rectype:
    NS;\nsubname: dns;priority: 2;address: ns2.domainhost. net.;rectype:
    NS;\nsubname: help;priority: 0;address: 67.22.36.4;rect ype: A;\nsubname:
    mail;priority: 0;address: domainname.net. ;rectype: CNAME;\nsubname :
    support;priorit y: 0;address: help.domainhost .net.;rectype:
    CNAME;\nsubname : text;priority: 0;address: \"tex value\";rectype : TXT;
    >
    Each record is separated by "\n". The columns are:
    >
    subname
    rectype
    priority
    address
    >
    I would appreciate very much if you could point me to the right direction
    how to parse this string efficiently.
    >
    Thank you,
    >
    Peter

    Comment

    • Peter Afonin

      #3
      Re: How to parse CSV file

      Thank you, Cor. I'll try to find them, had no luck so far, perhaps using
      wrong search terms.

      Peter

      "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
      news:6606A607-0EEA-42F3-8A6B-C684126E1389@mi crosoft.com...
      Peter,
      >
      Use OleDB for this about 1000.000 samples on Internet how to do this.
      >
      Cor
      >
      "Peter Afonin" <petera@gudzon. netschreef in bericht
      news:%23Ok9cIVu IHA.4476@TK2MSF TNGP06.phx.gbl. ..
      >Hello,
      >>
      >I need to parse a string that returns the domain DNS records and to put
      >this data into a DataTable. I don't have much experience in parsing
      >strings, so I'm not aware of the efficient way how to do this. I've found
      >some examples of CSV parser classes, but not sure how to use them in my
      >case.
      >>
      >Here is an example of the string:
      >>
      >RRs\nsubname : @;priority: 5;address: mail.domainname .net.;rectype:
      >MX;\nsubname : dns;priority: 1;address: ns1.domainhost. net.;rectype:
      >NS;\nsubname : dns;priority: 2;address: ns2.domainhost. net.;rectype:
      >NS;\nsubname : help;priority: 0;address: 67.22.36.4;rect ype: A;\nsubname:
      >mail;priorit y: 0;address: domainname.net. ;rectype: CNAME;\nsubname :
      >support;priori ty: 0;address: help.domainhost .net.;rectype:
      >CNAME;\nsubnam e: text;priority: 0;address: \"tex value\";rectype : TXT;
      >>
      >Each record is separated by "\n". The columns are:
      >>
      >subname
      >rectype
      >priority
      >address
      >>
      >I would appreciate very much if you could point me to the right direction
      >how to parse this string efficiently.
      >>
      >Thank you,
      >>
      >Peter
      >

      Comment

      • Marc Gravell

        #4
        Re: How to parse CSV file

        While OLEDB is an option, you can also do it (more efficiently) in pure
        managed code:


        I've used this one before - it works great.

        (search terms were: csv reader C#)

        Marc

        Comment

        • Ignacio Machin ( .NET/ C# MVP )

          #5
          Re: How to parse CSV file

          On May 18, 9:45 pm, "Peter Afonin" <pet...@gudzon. netwrote:
          Hello,
          >
          I need to parse a string that returns the domain DNS records and to put this
          data into a DataTable. I don't have much experience in parsing strings, so
          I'm not aware of the efficient way how to do this. I've found some examples
          of CSV parser classes, but not sure how to use them in my case.
          >
          Here is an example of the string:
          >
          RRs\nsubname: @;priority: 5;address: mail.domainname .net.;rectype:
          MX;\nsubname: dns;priority: 1;address: ns1.domainhost. net.;rectype:
          NS;\nsubname: dns;priority: 2;address: ns2.domainhost. net.;rectype:
          NS;\nsubname: help;priority: 0;address: 67.22.36.4;rect ype: A;\nsubname:
          mail;priority: 0;address: domainname.net. ;rectype: CNAME;\nsubname :
          support;priorit y: 0;address: help.domainhost .net.;rectype: CNAME;\nsubname :
          text;priority: 0;address: \"tex value\";rectype : TXT;
          >
          Each record is separated by "\n". The columns are:
          >
          subname
          rectype
          priority
          address
          >
          I would appreciate very much if you could point me to the right direction
          how to parse this string efficiently.
          >
          Thank you,
          >
          Peter
          Hi,

          Go to opennetcf.org and download the source code of the library they
          offer, they provide a very straigforward provider

          Comment

          • Peter Afonin

            #6
            Re: How to parse CSV file

            Thank you very much everyone, I'll try it.

            Peter

            "Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin @gmail.comwrote in
            message
            news:eb08ee37-6459-4bd9-ac03-82d7ae095073@f3 6g2000hsa.googl egroups.com...
            On May 18, 9:45 pm, "Peter Afonin" <pet...@gudzon. netwrote:
            Hello,
            >
            I need to parse a string that returns the domain DNS records and to put
            this
            data into a DataTable. I don't have much experience in parsing strings, so
            I'm not aware of the efficient way how to do this. I've found some
            examples
            of CSV parser classes, but not sure how to use them in my case.
            >
            Here is an example of the string:
            >
            RRs\nsubname: @;priority: 5;address: mail.domainname .net.;rectype:
            MX;\nsubname: dns;priority: 1;address: ns1.domainhost. net.;rectype:
            NS;\nsubname: dns;priority: 2;address: ns2.domainhost. net.;rectype:
            NS;\nsubname: help;priority: 0;address: 67.22.36.4;rect ype: A;\nsubname:
            mail;priority: 0;address: domainname.net. ;rectype: CNAME;\nsubname :
            support;priorit y: 0;address: help.domainhost .net.;rectype:
            CNAME;\nsubname :
            text;priority: 0;address: \"tex value\";rectype : TXT;
            >
            Each record is separated by "\n". The columns are:
            >
            subname
            rectype
            priority
            address
            >
            I would appreciate very much if you could point me to the right direction
            how to parse this string efficiently.
            >
            Thank you,
            >
            Peter
            Hi,

            Go to opennetcf.org and download the source code of the library they
            offer, they provide a very straigforward provider

            Comment

            • Cor Ligthert[MVP]

              #7
              Re: How to parse CSV file

              Peter,

              I saw it, all those samples are in Visual Basic, basicly it is this, have a
              look at the extened properties as you have problems.

              I just have copied this somewhere from Internet I did not test it, but I see
              at first sight no problems, it is quiet standard.

              \\\
              string path = @"C:\TEST\";
              System.Data.Ole Db.OleDbConnect ion conn = new
              System.Data.Ole Db.OleDbConnect ion("Provider=M icrosoft.Jet.OL EDB.4.0;Data
              Source=" + path + ";Extended
              Properties=\"te xt;HDR=No;IMEX= 1;FMT=Delimited \"");
              System.Data.Ole Db.OleDbCommand cmd = new
              System.Data.Ole Db.OleDbCommand ("SELECT * FROM sample.csv", conn);
              cmd.Connection = conn;
              System.Data.Ole Db.OleDbDataAda pter adapter = new
              System.Data.Ole Db.OleDbDataAda pter(cmd);
              System.Data.Dat aSet ds = new DataSet();
              conn.Open();
              adapter.Fill(ds );
              conn.Close();

              ///

              Cor

              "Peter Afonin" <petera@gudzon. netschreef in bericht
              news:%23EHadgXu IHA.1220@TK2MSF TNGP04.phx.gbl. ..
              Thank you, Cor. I'll try to find them, had no luck so far, perhaps using
              wrong search terms.
              >
              Peter
              >
              "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
              news:6606A607-0EEA-42F3-8A6B-C684126E1389@mi crosoft.com...
              >Peter,
              >>
              >Use OleDB for this about 1000.000 samples on Internet how to do this.
              >>
              >Cor
              >>
              >"Peter Afonin" <petera@gudzon. netschreef in bericht
              >news:%23Ok9cIV uIHA.4476@TK2MS FTNGP06.phx.gbl ...
              >>Hello,
              >>>
              >>I need to parse a string that returns the domain DNS records and to put
              >>this data into a DataTable. I don't have much experience in parsing
              >>strings, so I'm not aware of the efficient way how to do this. I've
              >>found some examples of CSV parser classes, but not sure how to use them
              >>in my case.
              >>>
              >>Here is an example of the string:
              >>>
              >>RRs\nsubnam e: @;priority: 5;address: mail.domainname .net.;rectype:
              >>MX;\nsubnam e: dns;priority: 1;address: ns1.domainhost. net.;rectype:
              >>NS;\nsubnam e: dns;priority: 2;address: ns2.domainhost. net.;rectype:
              >>NS;\nsubnam e: help;priority: 0;address: 67.22.36.4;rect ype: A;\nsubname:
              >>mail;priority : 0;address: domainname.net. ;rectype: CNAME;\nsubname :
              >>support;prior ity: 0;address: help.domainhost .net.;rectype:
              >>CNAME;\nsubna me: text;priority: 0;address: \"tex value\";rectype : TXT;
              >>>
              >>Each record is separated by "\n". The columns are:
              >>>
              >>subname
              >>rectype
              >>priority
              >>address
              >>>
              >>I would appreciate very much if you could point me to the right
              >>direction how to parse this string efficiently.
              >>>
              >>Thank you,
              >>>
              >>Peter
              >>
              >

              Comment

              • Peter Afonin

                #8
                Re: How to parse CSV file

                Thank you, Cor.

                I've tried this and some other examples. Unfortunately, I'm not dealing
                exactly with CSV file, I'm dealing with a string, although I can probably
                create a CSV file out of it using Stream. I'm trying to do this, but I'm not
                sure that I'm going the right way. Here is my string again:

                RRs\nsubname: @;priority: 5;address: mail.russianala ska.ru.;rectype :
                MX;\nsubname: dns;priority: 1;address: ns1.seattlehost .net.;rectype:
                NS;\nsubname: dns;priority: 2;address: ns2.seattlehost .net.;rectype:
                NS;\nsubname: help;priority: 0;address: 67.18.36.3;rect ype: A;\nsubname:
                mail;priority: 0;address: russianalaska.r u.;rectype: CNAME;\nsubname :
                support;priorit y: 0;address: help.seattlehos t.net.;rectype: CNAME;\nsubname :
                text;priority: 0;address: \"tex value\";rectype : TXT;\nSRV\nserv ice:
                _sip._udp;recty pe: SRV;priority: 1;weight: 0;port: 5060;target:
                sip.russianalas ka.ru.;\n

                Peter

                "Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
                news:3ED69A9E-2371-4245-9E1E-38B0517A3BEE@mi crosoft.com...
                Peter,
                >
                I saw it, all those samples are in Visual Basic, basicly it is this, have
                a look at the extened properties as you have problems.
                >
                I just have copied this somewhere from Internet I did not test it, but I
                see at first sight no problems, it is quiet standard.
                >
                \\\
                string path = @"C:\TEST\";
                System.Data.Ole Db.OleDbConnect ion conn = new
                System.Data.Ole Db.OleDbConnect ion("Provider=M icrosoft.Jet.OL EDB.4.0;Data
                Source=" + path + ";Extended
                Properties=\"te xt;HDR=No;IMEX= 1;FMT=Delimited \"");
                System.Data.Ole Db.OleDbCommand cmd = new
                System.Data.Ole Db.OleDbCommand ("SELECT * FROM sample.csv", conn);
                cmd.Connection = conn;
                System.Data.Ole Db.OleDbDataAda pter adapter = new
                System.Data.Ole Db.OleDbDataAda pter(cmd);
                System.Data.Dat aSet ds = new DataSet();
                conn.Open();
                adapter.Fill(ds );
                conn.Close();
                >
                ///
                >
                Cor
                >
                "Peter Afonin" <petera@gudzon. netschreef in bericht
                news:%23EHadgXu IHA.1220@TK2MSF TNGP04.phx.gbl. ..
                >
                >Thank you, Cor. I'll try to find them, had no luck so far, perhaps using
                >wrong search terms.
                >>
                >Peter
                >>
                >"Cor Ligthert[MVP]" <notmyfirstname @planet.nlwrote in message
                >news:6606A60 7-0EEA-42F3-8A6B-C684126E1389@mi crosoft.com...
                >>Peter,
                >>>
                >>Use OleDB for this about 1000.000 samples on Internet how to do this.
                >>>
                >>Cor
                >>>
                >>"Peter Afonin" <petera@gudzon. netschreef in bericht
                >>news:%23Ok9cI VuIHA.4476@TK2M SFTNGP06.phx.gb l...
                >>>Hello,
                >>>>
                >>>I need to parse a string that returns the domain DNS records and to put
                >>>this data into a DataTable. I don't have much experience in parsing
                >>>strings, so I'm not aware of the efficient way how to do this. I've
                >>>found some examples of CSV parser classes, but not sure how to use them
                >>>in my case.
                >>>>
                >>>Here is an example of the string:
                >>>>
                >>>RRs\nsubname : @;priority: 5;address: mail.domainname .net.;rectype:
                >>>MX;\nsubname : dns;priority: 1;address: ns1.domainhost. net.;rectype:
                >>>NS;\nsubname : dns;priority: 2;address: ns2.domainhost. net.;rectype:
                >>>NS;\nsubname : help;priority: 0;address: 67.22.36.4;rect ype:
                >>>A;\nsubnam e: mail;priority: 0;address: domainname.net. ;rectype:
                >>>CNAME;\nsubn ame: support;priorit y: 0;address:
                >>>help.domainh ost.net.;rectyp e: CNAME;\nsubname : text;priority:
                >>>0;address: \"tex value\";rectype : TXT;
                >>>>
                >>>Each record is separated by "\n". The columns are:
                >>>>
                >>>subname
                >>>rectype
                >>>priority
                >>>address
                >>>>
                >>>I would appreciate very much if you could point me to the right
                >>>direction how to parse this string efficiently.
                >>>>
                >>>Thank you,
                >>>>
                >>>Peter
                >>>
                >>
                >

                Comment

                • Marc Gravell

                  #9
                  Re: How to parse CSV file

                  Well, as you say: that certainly isn't CSV ;-p

                  The first thing to do would be to understand the rules... especially
                  regarding things like semicolons in escaped literals - but assuming
                  something simple, you could do this with standard string functions:

                  string data = "RRs\nsubna me: @;priority: 5;address:
                  mail.russianala ska.ru.;rectype : MX;\nsubname: dns;priority: 1;address:
                  ns1.seattlehost .net.;rectype: NS;\nsubname: dns;priority: 2;address:
                  ns2.seattlehost .net.;rectype: NS;\nsubname: help;priority: 0;address:
                  67.18.36.3;rect ype: A;\nsubname: mail;priority: 0;address:
                  russianalaska.r u.;rectype: CNAME;\nsubname : support;priorit y:
                  0;address: help.seattlehos t.net.;rectype: CNAME;\nsubname :
                  text;priority: 0;address: \"tex value\";rectype : TXT;\nSRV\nserv ice:
                  _sip._udp;recty pe: SRV;priority: 1;weight: 0;port: 5060;target:
                  sip.russianalas ka.ru.;\n";
                  string[] lines = data.Split('\n' );
                  for (int i = 1; i < lines.Length; i++)
                  { // (skip first line)
                  string[] pairs = lines[i].Split(';');
                  foreach (string pair in pairs)
                  {
                  int index = pair.IndexOf(': ');
                  if (index < 0) continue;
                  string key = pair.Substring( 0, index).Trim(),
                  value = pair.Substring( index + 1).Trim();

                  Console.WriteLi ne("{0}: {1}={2}", i, key, value);
                  }
                  }

                  Comment

                  • Peter Afonin

                    #10
                    Re: How to parse CSV file

                    Thank you, Marc, I'll try. I thought it might be easier to try to convert it
                    to CSV, but I guess not...

                    Peter

                    "Marc Gravell" <marc.gravell@g mail.comwrote in message
                    news:5e3b045d-f365-450f-8815-e08359605c36@24 g2000hsh.google groups.com...
                    Well, as you say: that certainly isn't CSV ;-p
                    >
                    The first thing to do would be to understand the rules... especially
                    regarding things like semicolons in escaped literals - but assuming
                    something simple, you could do this with standard string functions:
                    >
                    string data = "RRs\nsubna me: @;priority: 5;address:
                    mail.russianala ska.ru.;rectype : MX;\nsubname: dns;priority: 1;address:
                    ns1.seattlehost .net.;rectype: NS;\nsubname: dns;priority: 2;address:
                    ns2.seattlehost .net.;rectype: NS;\nsubname: help;priority: 0;address:
                    67.18.36.3;rect ype: A;\nsubname: mail;priority: 0;address:
                    russianalaska.r u.;rectype: CNAME;\nsubname : support;priorit y:
                    0;address: help.seattlehos t.net.;rectype: CNAME;\nsubname :
                    text;priority: 0;address: \"tex value\";rectype : TXT;\nSRV\nserv ice:
                    _sip._udp;recty pe: SRV;priority: 1;weight: 0;port: 5060;target:
                    sip.russianalas ka.ru.;\n";
                    string[] lines = data.Split('\n' );
                    for (int i = 1; i < lines.Length; i++)
                    { // (skip first line)
                    string[] pairs = lines[i].Split(';');
                    foreach (string pair in pairs)
                    {
                    int index = pair.IndexOf(': ');
                    if (index < 0) continue;
                    string key = pair.Substring( 0, index).Trim(),
                    value = pair.Substring( index + 1).Trim();
                    >
                    Console.WriteLi ne("{0}: {1}={2}", i, key, value);
                    }
                    }

                    Comment

                    • Peter Afonin

                      #11
                      Re: How to parse CSV file

                      Marc, thank you very much again, this worked very well for me.

                      Peter

                      "Marc Gravell" <marc.gravell@g mail.comwrote in message
                      news:5e3b045d-f365-450f-8815-e08359605c36@24 g2000hsh.google groups.com...
                      Well, as you say: that certainly isn't CSV ;-p
                      >
                      The first thing to do would be to understand the rules... especially
                      regarding things like semicolons in escaped literals - but assuming
                      something simple, you could do this with standard string functions:
                      >
                      string data = "RRs\nsubna me: @;priority: 5;address:
                      mail.russianala ska.ru.;rectype : MX;\nsubname: dns;priority: 1;address:
                      ns1.seattlehost .net.;rectype: NS;\nsubname: dns;priority: 2;address:
                      ns2.seattlehost .net.;rectype: NS;\nsubname: help;priority: 0;address:
                      67.18.36.3;rect ype: A;\nsubname: mail;priority: 0;address:
                      russianalaska.r u.;rectype: CNAME;\nsubname : support;priorit y:
                      0;address: help.seattlehos t.net.;rectype: CNAME;\nsubname :
                      text;priority: 0;address: \"tex value\";rectype : TXT;\nSRV\nserv ice:
                      _sip._udp;recty pe: SRV;priority: 1;weight: 0;port: 5060;target:
                      sip.russianalas ka.ru.;\n";
                      string[] lines = data.Split('\n' );
                      for (int i = 1; i < lines.Length; i++)
                      { // (skip first line)
                      string[] pairs = lines[i].Split(';');
                      foreach (string pair in pairs)
                      {
                      int index = pair.IndexOf(': ');
                      if (index < 0) continue;
                      string key = pair.Substring( 0, index).Trim(),
                      value = pair.Substring( index + 1).Trim();
                      >
                      Console.WriteLi ne("{0}: {1}={2}", i, key, value);
                      }
                      }

                      Comment

                      • Martin M

                        #12
                        Re: How to parse CSV file

                        OrchidGrid supports parsing data from CVS file.

                        Could find it from www.springsys.com


                        Hello,
                        >
                        I need to parse a string that returns the domain DNS records and to put
                        this data into a DataTable. I don't have much experience in parsing
                        strings, so I'm not aware of the efficient way how to do this. I've found
                        some examples of CSV parser classes, but not sure how to use them in my
                        case.
                        >
                        Here is an example of the string:
                        >
                        RRs\nsubname: @;priority: 5;address: mail.domainname .net.;rectype:
                        MX;\nsubname: dns;priority: 1;address: ns1.domainhost. net.;rectype:
                        NS;\nsubname: dns;priority: 2;address: ns2.domainhost. net.;rectype:
                        NS;\nsubname: help;priority: 0;address: 67.22.36.4;rect ype: A;\nsubname:
                        mail;priority: 0;address: domainname.net. ;rectype: CNAME;\nsubname :
                        support;priorit y: 0;address: help.domainhost .net.;rectype:
                        CNAME;\nsubname : text;priority: 0;address: \"tex value\";rectype : TXT;
                        >
                        Each record is separated by "\n". The columns are:
                        >
                        subname
                        rectype
                        priority
                        address
                        >
                        I would appreciate very much if you could point me to the right direction
                        how to parse this string efficiently.
                        >
                        Thank you,
                        >
                        Peter

                        Comment

                        Working...