Using data from csv file several times

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ennoil
    New Member
    • May 2010
    • 11

    Using data from csv file several times

    I am new to Python. I am trying to port a BASH script I wrote to Python so I can learn the language.
    I have a csv file with 5 columns. The first column is a host name. Second through 5th have different attributes of the host (environment, use, OS, architecture). I want to allow the user to pick one type from each column and output the hostnames that match the users request (i.e. list all hostnames that are Oracle servers in Production running Red Hat 64 bit). The user will pick from a text menu that is built from the csv file.
    The initial menu lists each of the 2nd through 5th column names. The user can pick one (i.e. Environment) column, the csv file gets read in and picks the environment type (TEST, QA, PROD...). The user picks one.
    Now I have the problem, want to go back to the initial menu and let the user pick another column. Do I have to read the file again and pick out the new column choice or can I keep the initial file read in memory (this file is small with only about 60 rows) and just pick the column I want from that?
    An example row looks like:
    <hostname>,PROD ,Oracle_Server, Red_Hat,64
    Thanks,
    John

    PS - The ideal thing would be to create a database from the csv file then write a select statement to pick out the hostnames i need but for some reason it was decided not to install sqlite on this machine...I wish there was....
    Last edited by ennoil; May 28 '10, 05:47 PM. Reason: Added PS...
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    The CSV file can easily be read in memory. If the hostnames are unique, you could use the hostnames as keywords of a dictionary as in:
    Code:
    dd = {'hostname1': ['PROD','Oracle_Server','Red_Hat','64'], 'hostname2': ['PROD','Oracle_Server','Red_Hat','64']}
    You could search through the dictionary like this:
    Code:
    >>> dd = {'hostname1': ['PROD','Oracle_Server','Red_Hat','64'], 'hostname2': ['PROD','Oracle_Server','Red_Hat','64']}
    >>> keyw='Red_Hat'
    >>> for key in dd:
    ... 	if keyw in dd[key]:
    ... 		print key
    ... 		
    hostname1
    hostname2
    >>>
    Sixty records is small enough to do this way. If you had thousands of records, you should consider a database like MySQL.

    Comment

    • ennoil
      New Member
      • May 2010
      • 11

      #3
      Would something like:

      Code:
      import csv
      
      f = open(".servers.txt", "r")
      reader = csv.reader(f)
      for row in reader:
        data.append(row[0]: [row[1], row[2], row[3], row[4])
      f.close
      work?
      Thanks

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        What is data supposed to be? A list or a dictionary? It appears you are confusing the two object types. Try something like this (untested):
        Code:
        import csv
        f = open('data.csv', 'rb')
        reader = csv.reader(f)
        dd = {}
        for item in reader:
            dd[item[0]] = item[1:]
        f.close()

        Comment

        • ennoil
          New Member
          • May 2010
          • 11

          #5
          Sorry, forgot to declare data before...
          data = []
          (Is that declaring it as a dictionary? Is there a table that shows what each are ([], {}...)
          John
          Last edited by ennoil; May 28 '10, 07:22 PM. Reason: I am never happy with the first one...

          Comment

          • dwblas
            Recognized Expert Contributor
            • May 2008
            • 626

            #6
            (this file is small with only about 60 rows
            Just read it into memory and search the entire list for a file this small.
            Code:
            ##data_list = open(file_name, "r").readlines()
            ##
            ##   simulate reading the file
            data_list = []
            for x in range(60):
               data_list.append("hostname-%d, prod-%d, server-%d, red_hat-%d, 64\n" % \
                               (x, x, x, x))
            #
            ## split each record and remove spaces
            list_of_lists = []
            for rec in data_list:
                junk_list = []
                substrs=rec.split(",")
                for s in substrs:
                    junk_list.append(s.strip())
                list_of_lists.append(junk_list)
            #
            ##   find "PROD" = 5
            for rec in list_of_lists:
                if rec[1] == 'prod-5':
                    print rec
            ##   find "server" = 55
            for rec in list_of_lists:
                if rec[2] == 'server-55':
                    print rec

            Comment

            Working...