csv file how to modify the row

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • muttu2244@yahoo.com

    #1

    csv file how to modify the row

    Hi everybody


    Am trying to read a csv file "temp.csv", which has the below info,


    compName macAddr ipAddr
    opSys


    Chris-Dev 0003469F44CC 10.160.24.226 Microsoft
    Windows XP
    Professional
    Shivayogi-Dev 000D5234F44C 10.160.24.136 Microsoft Windows XP
    Professional
    John-test 000D123F44CC 10.160.24.216 Microsoft
    Windows XP
    Professional Steve-Dev 000D123F55CC 10.160.24.116
    Microsoft
    Windows XP Professional


    am trying to read it in the following way--


    [color=blue][color=green][color=darkred]
    >>> import csv
    >>> infoFile = open ('c:\\temp.csv' ,'r')
    >>> rdr = csv.reader(info File)
    >>> for row in rdr:[/color][/color][/color]


    .... if row[0] == infnList[0]:
    .... if not row[1] == infnList[1] or not row[2] ==
    infnList[2]:

    now am comparing my comp Name with the "compName" fields, and if it
    matches i ll then compare for the "mac address" and "ip address". if
    they are not matching with my system, i have to modify them there
    itself, i mean i have to update "mac address" and "ip address" in the
    same row itself, i dont want to append the information with another row

    to the "temp.csv" file. otherwise it will have two similar computer
    names for two rows, which i dont want to happen.


    please give me the ways how i can work on this.


    thanks in advance
    yogi

  • Steven D'Aprano

    #2
    Re: csv file how to modify the row

    On Tue, 27 Dec 2005 14:45:16 -0800, muttu2244 wrote:
    [color=blue]
    > Hi everybody
    >
    >
    > Am trying to read a csv file "temp.csv", which has the below info,
    >
    >
    > compName macAddr ipAddr
    > opSys
    >
    >
    > Chris-Dev 0003469F44CC 10.160.24.226 Microsoft
    > Windows XP Professional[/color]

    [snip]

    Why are you calling it a Comma Separated Values file when there are no
    commas separating values in it?

    [color=blue]
    > am trying to read it in the following way--
    >
    >
    >[color=green][color=darkred]
    >>>> import csv
    >>>> infoFile = open ('c:\\temp.csv' ,'r')
    >>>> rdr = csv.reader(info File)
    >>>> for row in rdr:[/color][/color]
    >
    >
    > ... if row[0] == infnList[0]:
    > ... if not row[1] == infnList[1] or not row[2] ==
    > infnList[2]:[/color]


    Should we know what infnList is, or shall we guess?

    What does your code do next?
    if condition: (do what?) else: (do what?)

    [color=blue]
    > now am comparing my comp Name with the "compName" fields, and if it
    > matches i ll then compare for the "mac address" and "ip address". if
    > they are not matching with my system, i have to modify them there
    > itself, i mean i have to update "mac address" and "ip address" in the
    > same row itself, i dont want to append the information with another row[/color]

    If I have understood what you are saying, you only need to modify one
    single row in the file, the row that matches your personal computer. Why
    don't you just open the data file in Notepad or Wordpad, use "Find" to
    search for the row you want, and modify it by hand?

    If that is not what you need to do, would you like to try explaining what
    you need to do a little more carefully? Do you have to modify every line,
    or just some? If the data in your temp.csv file is suspect, where are you
    getting the good data from?

    [color=blue]
    > to the "temp.csv" file. otherwise it will have two similar computer
    > names for two rows, which i dont want to happen.[/color]

    Why not? Chris-Dev and Kris-Dev are similar but not the same, shouldn't
    they get different rows?


    Some questions for you to think about:

    1. Is the data file small enough to all fit into memory (less than, say,
    ten megabytes)?

    2. Can you follow this algorithm?

    open data file for reading
    read everything into a list of strings
    close data file
    walk the list modifying each string if it needs fixing
    open data file for writing
    write the list of strings
    close data file


    3. What about this algorithm?

    open data file for reading
    open temporary update file for writing
    for each record in data file:
    is record correct?
    yes: write it unchanged to update file
    no: fix the record and write it to update file
    close data file
    close update file
    delete obsolete data file
    rename update file to data file




    --
    Steven.

    Comment

    • Alex Martelli

      #3
      Re: csv file how to modify the row

      Steven D'Aprano <steve@REMOVETH IScyber.com.au> wrote:
      [color=blue]
      > Why are you calling it a Comma Separated Values file when there are no
      > commas separating values in it?[/color]

      It's common usage -- the Python standard library's csv also lets you
      parse files where the separator is not a comma -- you just need to have
      an explicit 'delimiter' (the comma is just the default value for this);
      indeed, class csv.excel_tab has an explicit delimiter set to '\t'.


      Alex

      Comment

      • Steven D'Aprano

        #4
        Re: csv file how to modify the row

        On Tue, 27 Dec 2005 19:24:57 -0800, Alex Martelli wrote:
        [color=blue]
        > Steven D'Aprano <steve@REMOVETH IScyber.com.au> wrote:
        >[color=green]
        >> Why are you calling it a Comma Separated Values file when there are no
        >> commas separating values in it?[/color]
        >
        > It's common usage[/color]

        Well you learn something new every day. Of course I know about
        tab-delimited files and what-not, but that's the first time I've come
        across somebody calling one a CSV file.


        --
        Steven.

        Comment

        • Alex Martelli

          #5
          Re: csv file how to modify the row

          Steven D'Aprano <steve@REMOVETH IScyber.com.au> wrote:
          [color=blue]
          > On Tue, 27 Dec 2005 19:24:57 -0800, Alex Martelli wrote:
          >[color=green]
          > > Steven D'Aprano <steve@REMOVETH IScyber.com.au> wrote:
          > >[color=darkred]
          > >> Why are you calling it a Comma Separated Values file when there are no
          > >> commas separating values in it?[/color]
          > >
          > > It's common usage[/color]
          >
          > Well you learn something new every day. Of course I know about
          > tab-delimited files and what-not, but that's the first time I've come
          > across somebody calling one a CSV file.[/color]

          I realize that calling "a csv file" something that uses a delimiter
          other than comma may feel weird, but we've had csv in the standard
          Python library for a while, and it's hard to think of any other wholly
          generic monicker. Maybe you can think of the "c" as standing for
          "character" (not sure the delimiter has to be ONE character, tho;-)...


          Alex

          Comment

          Working...