CSV Reader

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike P

    CSV Reader

    Hi All,

    I want to read in a CSV file, but then write out a new CSV file from a
    given line..

    I'm using the CSV reader and have the the line where i want to start
    writing the new file from begins with
    "Transactio n ID",

    i thought it should be something along the lines of below.. obvioulsy
    this doesn't work but any help would be great.

    import csv
    f = file(working_CS V, 'rb')
    new_data = 0 # a counter to find where the line starts with
    "Transactio n ID"
    reader = csv.reader(f)
    for data in reader:
    read data file

    write new CSV

    Cheers

    Mike

  • Larry Bates

    #2
    Re: CSV Reader

    Mike P wrote:
    Hi All,
    >
    I want to read in a CSV file, but then write out a new CSV file from a
    given line..
    >
    I'm using the CSV reader and have the the line where i want to start
    writing the new file from begins with
    "Transactio n ID",
    >
    i thought it should be something along the lines of below.. obvioulsy
    this doesn't work but any help would be great.
    >
    import csv
    f = file(working_CS V, 'rb')
    new_data = 0 # a counter to find where the line starts with
    "Transactio n ID"
    reader = csv.reader(f)
    for data in reader:
    read data file
    >
    write new CSV
    >
    Cheers
    >
    Mike
    >
    What part "obviously" doesn't work? Try something, post any tracebacks and we
    will try to help. Don't ask others to write your code for you without actually
    trying it yourself. It appears you are on the right track.

    -Larry

    Comment

    • Mike P

      #3
      Re: CSV Reader

      Cheers for the help, the second way looked to be the best in the end,
      and thanks for the boolean idea

      Mike



      working_CSV = "//filer/common/technical/Research/E2C/Template_CSV/
      DFAExposureToCo nversionQueryTo ol.csv"

      save_file = open("//filer/common/technical/Research/E2C/Template_CSV/
      CSV_Data2.csv", "w")

      CSV_Data = open(working_CS V)
      data = CSV_Data.readli nes()
      flag=False
      for record in data:
      if record.startswi th('"Transactio n ID"'):
      flag=True
      if flag:
      save_file.write (record)
      save_file.close ()

      Comment

      • Gabriel Genellina

        #4
        Re: CSV Reader

        En Mon, 11 Feb 2008 14:41:54 -0200, Mike P
        <michael.pearma in@tangozebra.c omescribi�:
        CSV_Data = open(working_CS V)
        data = CSV_Data.readli nes()
        flag=False
        for record in data:
        if record.startswi th('"Transactio n ID"'):
        [...]
        Files are already iterable by lines. There is no need to use readlines(),
        and you can avoid the already menctioned potential slowdown problem. Just
        remove the data=CSV_data.r eadlines() line, and change that for statement
        to be:
        for record in CSV_Data:

        Reading the style guide may be beneficial:
        This document gives coding conventions for the Python code comprising the standard library in the main Python distribution. Please see the companion informational PEP describing style guidelines for the C code in the C implementation of Python.


        --
        Gabriel Genellina

        Comment

        • Mike P

          #5
          Re: CSV Reader

          I did just try to post, but it doesn't look like it has appeared?

          I've used your advice Andrew and tried to use the CSV module, but now
          it doesn't seem to pick up the startswith command?
          Is this because of the way the CSV module is reading the data in?
          I've looked into the module description but i can't find anything that
          i hould be using?

          Can anyone offer an advice?

          Cheers again

          Mike

          working_CSV = "//filer/common/technical/Research/E2C/Template_CSV/
          DFAExposureToCo nversionQueryTo ol.csv"

          save_file = "//filer/common/technical/Research/E2C/Template_CSV/
          CSV_Data2.csv"

          start_line=Fals e
          import csv
          reader = csv.reader(open (working_CSV, "rb"))
          writer = csv.writer(open (save_file, "wb"))
          for row in reader:
          if not start_line and record.startswi th("'Transactio n ID'"):
          start_line=True
          if start_line:
          print row
          writer.writerow s(rows)
          #writer.close()

          Comment

          • Mike P

            #6
            Re: CSV Reader

            just saw i needed to change record.startswi th to row.startswith
            but i get hte following traceback error

            Traceback (most recent call last):
            File "C:\Python25\Li b\site-packages\python win\pywin\frame work
            \scriptutils.py ", line 310, in RunScript
            exec codeObject in __main__.__dict __
            File "Y:\technical\R esearch\E2C\Tem plate_CSV\impor t CSV test.py",
            line 10, in <module>
            if not start_line and row.startswith( 'Transaction ID'):
            AttributeError: 'list' object has no attribute 'startswith'

            Comment

            • Chris

              #7
              Re: CSV Reader

              On Feb 12, 12:21 pm, Mike P <michael.pearm. ..@tangozebra.c omwrote:
              I did just try to post, but it doesn't look like it has appeared?
              >
              I've used your advice Andrew and tried to use the CSV module, but now
              it doesn't seem to pick up the startswith command?
              Is this because of the way the CSV module is reading the data in?
              I've looked into the module description but i can't find anything that
              i hould be using?
              >
              Can anyone offer an advice?
              >
              Cheers again
              >
              Mike
              >
              working_CSV = "//filer/common/technical/Research/E2C/Template_CSV/
              DFAExposureToCo nversionQueryTo ol.csv"
              >
              save_file = "//filer/common/technical/Research/E2C/Template_CSV/
              CSV_Data2.csv"
              >
              start_line=Fals e
              import csv
              reader = csv.reader(open (working_CSV, "rb"))
              writer = csv.writer(open (save_file, "wb"))
              for row in reader:
              if not start_line and record.startswi th("'Transactio n ID'"):
              start_line=True
              if start_line:
              print row
              writer.writerow s(rows)
              #writer.close()
              record won't have an attribute 'startswith' because record is a list
              and startswith is a function of a string.
              Also, your code isn't exactly clear on what you want to do, if it is
              just "Find the first occurence of Transaction ID and pump the file
              from then onwards into a new file" why not

              output = open('output_fi le.csv','wb')
              start_line = False
              for each_line in open('input_fil e.csv','rb'):
              if not start_line and each_line.start swith("'Transac tion ID'"):
              start_line = True
              if start_line:
              output.write( each_line )
              output.close()

              also, if you need a line number for any purposes, take a look at
              enumerate() and with that it will return a counter and your data, for
              eg. 'for (line_num, each_line) in enumerate(input _file):'. Counting
              starts @ zero though so you would need to add 1.

              Comment

              • Mike P

                #8
                Re: CSV Reader

                Hi Chris that's exactley what i wanted to do,

                Many thanks

                Comment

                • Mike P

                  #9
                  Re: CSV Reader

                  I was just trying to do it with the CSV module

                  Comment

                  • Gabriel Genellina

                    #10
                    Re: CSV Reader

                    En Tue, 12 Feb 2008 08:37:13 -0200, Mike P
                    <michael.pearma in@tangozebra.c omescribi�:
                    just saw i needed to change record.startswi th to row.startswith
                    but i get hte following traceback error
                    >
                    Traceback (most recent call last):
                    File "C:\Python25\Li b\site-packages\python win\pywin\frame work
                    \scriptutils.py ", line 310, in RunScript
                    exec codeObject in __main__.__dict __
                    File "Y:\technical\R esearch\E2C\Tem plate_CSV\impor t CSV test.py",
                    line 10, in <module>
                    if not start_line and row.startswith( 'Transaction ID'):
                    AttributeError: 'list' object has no attribute 'startswith'
                    The csv reader doesn't return complete lines, but a list of fields for
                    each line. If you want to check the first field, use:

                    if not start_line and row[0].startswith('Tr ansaction ID'):

                    --
                    Gabriel Genellina

                    Comment

                    Working...