Need help converting text to csv format

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Strout

    #16
    Re: Need help converting text to csv format

    On Nov 21, 2008, at 10:26 AM, MRAB wrote:
    The file will be closed automatically when the file object is
    garbage-collected.
    >
    CPython uses reference-counting, so the file object is garbage-
    collected as soon as there are no references to it.
    >
    Jython (and IronPython?) are garbage-collected in the background, so
    the file object is garbage-collected at some point (and you don't
    know when that will be!) when there are no longer any references to
    it.
    Thanks all for this clarification. That is an important distinction.
    (REALbasic uses reference-counting, so you can guarantee that the file
    will be closed as soon as it loses its last reference, but I see that
    we shouldn't count on that in the Python world.)

    Best,
    - Joe

    Comment

    • George Sakkis

      #17
      Re: Need help converting text to csv format

      On Nov 21, 11:05 am, Steve Holden <st...@holdenwe b.comwrote:
      George Sakkis wrote:
      On Nov 21, 10:18 am, Chuck Connors <don...@gmail.c omwrote:
      >
      Any help, pseudo code, or whatever push in the right direction would
      be most appreciated.  I am a novice Python programmer but I do have a
      good bit of PHP programming experience.
      >
      I'm wondering if PHP experience precludes the ability to use a search
      engine before asking for help...
      >
      I'm wondering why you bothered to write that. Next time, save yourself
      the ten seconds and just skip to the next message. The world will be a
      better place.
      RTFM and doing an elementary search is an even better way to making
      the world a better place.

      George

      Comment

      • Richard Riley

        #18
        Re: Need help converting text to csv format

        George Sakkis <george.sakkis@ gmail.comwrites :
        On Nov 21, 11:05 am, Steve Holden <st...@holdenwe b.comwrote:
        >George Sakkis wrote:
        On Nov 21, 10:18 am, Chuck Connors <don...@gmail.c omwrote:
        >>
        >Any help, pseudo code, or whatever push in the right direction would
        >be most appreciated.  I am a novice Python programmer but I do have a
        >good bit of PHP programming experience.
        >>
        I'm wondering if PHP experience precludes the ability to use a search
        engine before asking for help...
        >>
        >I'm wondering why you bothered to write that. Next time, save yourself
        >the ten seconds and just skip to the next message. The world will be a
        >better place.
        >
        RTFM and doing an elementary search is an even better way to making
        the world a better place.
        >
        George
        So you will be replying in this tone to each and every question which
        has an answer discoverable by Google and in depth knowledge of the "FM"
        then I assume?

        Comment

        • r0g

          #19
          Re: Need help converting text to csv format

          Chuck Connors wrote:
          Hey guys. I'm working on a little program to help my wife catalog her/
          our coupons. I found a good resource but need help formatting the
          text data so that I can import it into a mysql database. Here's the
          data format:
          >
          409220000003 Life Fitness Products $1 (12-13-08) (CVS)
          546500181141 Oust Air Sanitizer, any B1G1F up to $3.49 (1-17-09) .35
          each
          518000159258 Pillsbury Crescent Dinner Rolls, any .25 (2-14-09)
          518000550406 Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
          Cinnamon Rolls, etc. .40 (2-14-09)
          >
          The first value (large number) is the UPC, the next element is the
          coupon description, followed by a date in parenthesis. Those are the
          only three elements I am concerned with. Can someone help me in
          reformatting this:
          >
          409220000003 Life Fitness Products $1 (12-13-08) (CVS)
          546500181141 Oust Air Sanitizer, any B1G1F up to $3.49 (1-17-09) .35
          each
          518000159258 Pillsbury Crescent Dinner Rolls, any .25 (2-14-09)
          518000550406 Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
          Cinnamon Rolls, etc. .40 (2-14-09)
          >
          into something like this:
          >
          "409220000003", "Life Fitness Products $1","12-13-08"
          "546500181141", "Oust Air Sanitizer, any B1G1F up to $3.49","1-17-09"
          "518000159258", "Pillsbury Crescent Dinner Rolls, any .25","2-14-09"
          "518000550406", "Pillsbury Frozen Grands Biscuits, Cinnamon Rolls, Mini
          Cinnamon Rolls, etc. .40","2-14-09"
          >
          Any help, pseudo code, or whatever push in the right direction would
          be most appreciated. I am a novice Python programmer but I do have a
          good bit of PHP programming experience.
          >
          Thanks for your time....

          Hi Chuck,

          Don't be put off, not everyone here is rude and what you are asking
          isn't so trivial as to justify that flaming RTFM horsecrap. You are
          moving from php to python - this is cause for celebration! Welcome :-)

          Anyway I can see two ways of attacking this, you could either write a
          finite state machine or use regular expressions. My preference would be
          regular expressions.

          The following should do what you want although I haven't tested it much
          and you might want to lookout for irregularities in your source text
          like linebreaks etc...

          import re
          regex = re.compile("([0-9]{3,13}) ([^(]*) \(([^)]*)")

          for each in regex.findall(s ource_data):
          print '"'+each[0]+'","'+each[1]+'","'+each[2]+'"'


          I don't know if you already know regular expressions from some other
          language so I won't bother explaining this one other than to say you may
          note Pythons regex syntax is generally more liberal than many other
          languages implementations . If this is all new to you and you do need
          this explaining just let me know and I will happily run you through it.


          Also, you say your eventual target is a SQL database. If the database is
          local (or reachable via a network) then you are probably going to be
          better off using python to connect to it directly using the MySQLdb
          module...

          import MySQLdb

          db = MySQLdb.connect (host="localhos t",
          user="uname",
          passwd="pwd",
          db="dbname")

          cursor = db.cursor()
          cursor.execute( "INSERT YOU SQL QUERY HERE")
          db.commit ()


          Hope this helps,

          Roger.

          Comment

          • George Sakkis

            #20
            Re: Need help converting text to csv format

            On Nov 21, 2:01 pm, Richard Riley <rileyrg...@gma il.comwrote:
            George Sakkis <george.sak...@ gmail.comwrites :
            On Nov 21, 11:05 am, Steve Holden <st...@holdenwe b.comwrote:
            George Sakkis wrote:
            On Nov 21, 10:18 am, Chuck Connors <don...@gmail.c omwrote:
            >
            Any help, pseudo code, or whatever push in the right direction would
            be most appreciated.  I am a novice Python programmer but I do have a
            good bit of PHP programming experience.
            >
            I'm wondering if PHP experience precludes the ability to use a search
            engine before asking for help...
            >
            I'm wondering why you bothered to write that. Next time, save yourself
            the ten seconds and just skip to the next message. The world will be a
            better place.
            >
            RTFM and doing an elementary search is an even better way to making
            the world a better place.
            >
            George
            >
            So you will be replying in this tone to each and every question which
            has an answer discoverable by Google and in depth knowledge of the "FM"
            then I assume?
            No, only to those which is blatantly obvious that they didn't put any
            effort whatsoever to find an answer on their own; "python csv" has
            1,420,000 hits in Google, the first one linking to the csv module
            docs. Feel free to check out my 4-year posting history in c.l.py to
            get a more accurate idea of the usefulness and tone of my posts.

            George

            Comment

            • John Machin

              #21
              Re: Need help converting text to csv format

              On Nov 22, 7:38 am, George Sakkis <george.sak...@ gmail.comwrote:
              On Nov 21, 2:01 pm, Richard Riley <rileyrg...@gma il.comwrote:
              >
              >
              >
              George Sakkis <george.sak...@ gmail.comwrites :
              On Nov 21, 11:05 am, Steve Holden <st...@holdenwe b.comwrote:
              >George Sakkis wrote:
              On Nov 21, 10:18 am, Chuck Connors <don...@gmail.c omwrote:
              >
              >Any help, pseudo code, or whatever push in the right direction would
              >be most appreciated.  I am a novice Python programmer but I do have a
              >good bit of PHP programming experience.
              >
              I'm wondering if PHP experience precludes the ability to use a search
              engine before asking for help...
              >
              >I'm wondering why you bothered to write that. Next time, save yourself
              >the ten seconds and just skip to the next message. The world will bea
              >better place.
              >
              RTFM and doing an elementary search is an even better way to making
              the world a better place.
              >
              George
              >
              So you will be replying in this tone to each and every question which
              has an answer discoverable by Google and in depth knowledge of the "FM"
              then I assume?
              >
              No, only to those which is blatantly obvious that they didn't put any
              effort whatsoever to find an answer on their own; "python csv" has
              1,420,000 hits in Google, the first one linking to the csv module
              docs.
              If I had to write such a script in Python, I'd be using the re and csv
              modules in similar fashion to responses already posted. If I had to
              write it in PHP, it'd be my first PHP script, so let's go Googling:

              google("php regex") first hit:

              [yuk, 3 different toolkits; 3rd is PCRE, let's try that]

              google("php csv") 3rd hit points to:


              Doesn't seem too difficult to me.

              I'd be interested to hear from the OP what is his level of experience
              in PHP, how he would have approached the problem in PHP, and why he
              didn't look for similar ways in Python.

              Comment

              • r0g

                #22
                Re: Need help converting text to csv format

                >>>>>On Nov 21, 10:18 am, Chuck Connors <don...@gmail.c omwrote:
                >>>>>>Any help, pseudo code, or whatever push in the right direction would
                >>>>>>be most appreciated. I am a novice Python programmer but I do have a
                >>>>>>good bit of PHP programming experience.
                <snip>


                John Machin wrote:
                If I had to write such a script in Python, I'd be using the re and csv
                modules in similar fashion to responses already posted. If I had to
                write it in PHP, it'd be my first PHP script, so let's go Googling:
                >
                google("php regex") first hit:

                [yuk, 3 different toolkits; 3rd is PCRE, let's try that]
                >
                google("php csv") 3rd hit points to:

                >
                Doesn't seem too difficult to me.
                I'm sure it doesn't, it doesn't to me either, but only because we
                already know what he needs! Do you think if the OP knew all about
                regular expressions and understood that python has a handy csv module
                that he'd be asking this question here?

                The OP is used to PHP, a fairly different kettle of fish. Disgusting
                though PHP is it is many peoples first programming language, often in
                the unpleasant form of mod_php on a web server - clearly we are not
                dealing with a linux kernel hacker here.

                I don't think it's at all unreasonable to come here and ask for "help,
                pseudo code, or whatever push in the right direction", and I certainly
                don't see a need to start questioning his credentails.

                He needs to know about the re and csv modules. A minimal yet polite
                response might have been: Go and google for the re and csv modules. You
                could have typed that, saved yourself a few hundred keystrokes and
                helped someone in the process.

                If you can't muster even that level of enthusiasm or courtesy towards
                the noobs then you always have the option of not responding.

                :-/

                Roger.

                Comment

                • alex23

                  #23
                  Re: Need help converting text to csv format

                  On Nov 22, 2:28 am, Joe Strout <j...@strout.ne twrote:
                  A follow-up question here... is it really necessary to close things  
                  like files in Python?
                  Not if you use a context manager (2.6+, available in 2.5 from
                  __future__):

                  with open('data.csv' , 'wb') as csvfile:
                  writer = csv.writer(csvf ile)
                  ...

                  The file is guaranteed to be closed at the end of the with-statement
                  block.

                  Comment

                  Working...