#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Unhappy Python: CSV file After appending cells, find min and max of five different categories


    Hello everyone I have a question. I just now learning min and max.

    I'm having trouble in finding the min and max of five columns for each category

    Heres what I have:

    I moved a 5 columns of 26 column data from a csv file to a txt file.

    for example the appended cells for .csv are like

    Code:
    state          car      motorcycle   van        airplane       bike 
    Maine          35.5      8.1         5.7         21.0%         33.2%
    Michigan       47.9      9.1         5.5         20.40%        25.2%                   
    Washington     52.5      1.2         4.6          3.50%        24.7%                     
    Denver         21.8      20.5        5.3          2.90%        30.9%
    how do I get the min and max to look like this
    Code:
                       min                       max
    car             Denver:      21.8          Washington: 52.5   
    motor           Washington:  1.2           Denver:     20.5 
    van             Washington   4.6           Maine:       5.7 
    airplane        Denver       2.90%         Maine       21.0% 
    bike            Washington   24.7%         Maine      33.2%
    Here is what I have

    Code:
    import csv
    import string, re
    import operator
    
    output = []
    data = []
    csv_string = []
    data_file = []
    
    try:
        with open('data.csv', 'r') as csv_string:
             for line in csv_string:
                 cells = line.split(",")
                 output.append((cells[0], cells[1], cells[5], cells[7], cells[11], cells[13]))
                             for lines in output:
    
    
                #state = cells[0]
    
                zmin = cells[1]   #car = cells[1]
                ymin = cells[1]
                xmin = cells[5]   #motor = cells[5] 
                wmin = cells[5]
                vmin = cells[7]   #van = cells[7]
                zmax = cells[7]
                ymax = cells[11]  #airplane = cells[11]
                xmax = cells[11]
                wmax = cells[13]  #bike = cells[13] 
                vmax = cells[13]
    
            if cells[1] < xmin: 
                zmin = cells[1] 
            if cells[1] > xmax: 
                zmax = cells[1]
    
    
            if cells[5] < ymin: 
                ymin = cells[5]
            if cells[5] > ymax: 
                ymax = cells[5]
    
            if cells[7] < zmin:
                xmin = cells[7]
            if cells[7] > zmax: 
                xmax = cells[7] 
    
            if cells[11] < zmin:
                wmin = cells[11]
            if cells[11] > zmax: 
                wmax = cells[11]
    
            if cells[13] < zmin:
                vmin = cells[13]
            if cells[13] > zmax: 
                vmax = cells[13]
    
            outstring = ' '
        for item in output:
                for cell in item:
                    outstring += "{0:<35}".format(cell) #Width/Distance of each row
                outstring += "\n"
    
        print(outstring)
    
        print('Min: ',zmin,ymin,xmin,wmin,vmin)
    
        print('Max: ',state,zmax,ymax,xmax,wmax,vmax) 
    
    
      finally:
              f.close()
    
    try:    
        f_write = open('output.txt', 'w') #creates the file
        try:
            f_write.writelines(outstring)
    
      finally:
              f.close()
    I'm not sure what I am doing wrong. I been reading min and max but I don't understand how this applies in a .csv file while appending 5 columns.

    If anyone can offer some guidance, Thank you for your input.

    The program prints wrong numbers

    Code:
     print('Min: ',zmin,ymin,xmin,wmin,vmin)
          47.9,  8.1, 5.5, 20.40%, 25.2% 
     print('Max: ',state,zmax,ymax,xmax,wmax,vmax) 
          21.8, 9.1, 4.6, 20.40%, 30.9%
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    First of all, your code seems to have some indentation problems. The line "for lines in output:" is out of place, should it be placed further down?

    Further, I would represent your output as a map instead of list, maybe like this:

    Code:
    result = {'car':
                  { 'min': 0,
                    'max': 0 },
              'motor':
                  { 'min': 0,
                    'max': 0 },
              'van':
                  { 'min': 0,
                    'max': 0 },
              'airplane':
                  { 'min': 0,
                    'max': 0 },
              'bike':
                  { 'min': 0,
                    'max': 0 }
              }
    
    # Update results like this
    result['van']['min'] = 37
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Delhi
    Posts
    35
    Rep Power
    2
    Dear patroj.

    I have output has below. i wanna write output
    [' in FWD']
    ['0']
    ['1']
    ['11/3/2013 16', '19', '19']
    ['-27.74']
    ['-22.80']
    in excel sheet for every 10 min making column dir,mode,date,time,desire angle, actual angle.as done below showed example . Please let me know how to write it. what are changes made in below code



    output:

    value is ['calc dir', ' in FWD\r\nActuator On/OFF', '0\r\nMode', '1\r\nLocal Date & Time', '11/3/2013 16', '19', '19\r\nTracker Desired Angle', '-27.74\r\nTracker Actual Pos', '-22.80\r\n----------']
    [' in FWD']
    ['0']
    ['1']
    ['11/3/2013 16', '19', '19']
    ['-27.74']
    ['-22.80']


    Code:
    import serial
    import csv
    import os
    import time
    
    
    def main():
        pass
    
    if __name__ == '__main__':
        main()
        COUNT=0
        while(COUNT<=2):
          ser=serial.Serial()
          ser.port=12
          ser.baudrate=9600
          ser.open()
          str=ser.read(150)
          val=str.split(":")
          print "value is",val
          lines=str.split("\r\n")
          ser.close()
          COUNT=COUNT+1
        for lines in lines[:-1]:
                parts=lines.split(":")
                foo=open("new.txt","a+");
                foo.write("%s," % ":".join(parts[1:]))
                print parts[1:]
                foo.write("\n")
                foo.close()
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    I've posted an answer in your origianl thread. Please don't spam other threads with duplicate questions.

IMN logo majestic logo threadwatch logo seochat tools logo