Python - csv

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ozchadl
    New Member
    • Apr 2010
    • 26

    Python - csv

    I am trying to write a program in python 2.6 which does the following:
    prompts for input
    selects that item in a csv file
    displays only the data for that input

    There are a total of 5 locations
    A1, A2, A3,A4,A5

    ---
    I can get the input, and I can read the whole csv file, then it prints the date, location, and course, for all of the locations, I only require the "date", "location", and "course" for that input

    The input of say "A1" should only display the following:
    Date Location Course
    4-4-10 A1 web1
    5-4-10 A1 web2
    6-4-10 A1 web3
    ---------

    csv file - test.txt
    Date,Location,C ourse,Stage
    4-4-10,A1,web1,1
    4-4-10,A2,wev2,2
    5-4-10,A1,web2,2
    6-4-10,A1,web3,3
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You can test for membership with the in operator to tabulate the required data. Then it's a matter of formatting the output.
    Code:
    fn = "location.txt"
    location = "A1"
    output = []
    f = open(fn)
    labels = f.readline().strip().split(',')
    for line in f:
        lineList = line.strip().split(',')
        if location in lineList:
            output.append(lineList)
    print
    print "%s%s%s" % (labels[0].center(8),
                      labels[1].center(10),
                      labels[2].center(8))
    print "\n".join(["%s%s%s" % (s[0].center(8),
                                 s[1].center(10),
                                 s[2].center(8)) for s in output])
    Output:
    Code:
    >>> 
      Date   Location  Course 
     4-4-10     A1      web1  
     5-4-10     A1      web2  
     6-4-10     A1      web3  
    >>>

    Comment

    • ozchadl
      New Member
      • Apr 2010
      • 26

      #3
      Thanks for your help.


      It the csv file - test.txt had say "a1" and "A1", and the input was say "a1" it would only display that item for "a1" instead of both.

      When the file is being read, would this need to be converted to upper or lowercase for it to display the item for "a1" and "A1"

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Here are two ways to approach it:
        Convert location to upper case and line to upper case before splitting
        Check for membership of location upper and lower case in lineList

        Comment

        • ozchadl
          New Member
          • Apr 2010
          • 26

          #5
          Could you tell me how I write the data displayed from the original reply to a csv text file called output.txt
          If the input was a1, it woudl display this on screen and write it to a csv text file:
          Date Location Course
          4-4-10 A1 web1
          5-4-10 A1 web2
          6-4-10 A1 web3

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            The output you request would not be a CSV file, but an ascii text file. To write the output string shown in my example to a file:
            Code:
            outputList = []
            outputList.append("%s%s%s" % (labels[0].center(8),
                                          labels[1].center(10),
                                          labels[2].center(8)))
            outputList.append("\n".join(["%s%s%s" % (s[0].center(8),
                                                     s[1].center(10),
                                                     s[2].center(8)) for s in output]))
            
            f = open("output.txt", 'w')
            f.write("\n".join(outputList))
            f.close()

            Comment

            Working...