简体   繁体   中英

How to Extract specific row information from text file min,max,median and to delete a chosen line?

Glad to hopefully get some advice from some experts,anyway im trying to extract specific row data from a text file and manipulate it using python 3.6.The txt files format has 4 distinctive columns Company,Domain,Ipaddress,Connections :

  1. brandy www.brandy.com 12.12.12.12 245
  2. whiskey www.whiskey.com 24.24.24.24 490
  3. bourbon www.bourbon.ag 36.36.36.36 980
  4. vodka www.vodka.ussr 48.48.48.48 1960

Now i would like to get the mean,median,minimum,maximum from the last column which is 245,490,980,1960...i got a bit of help from my tutor with the mean function: Which i understand, but im having difficulty in defining the how to write the min,max,median without using imported libraries to do the work for you..this is getting to know how the code works, my understanding of the code is the top 5 lines of code parse the text file and strips the white space and splits it up into 4 columns which are written to a temporary linelist to be appended to connectionList , start the counter at zero, then do a for --> in loop, defining a variable conns to convert string into a integer at position 3 in the row and continuing down code to calculate the mean....now my confusion is how to get min,max,median from the connectionList, using manual coding not importing libraries in to do the work for you?...oh sorry to be asking all these questions, one last one how would you delete say line 2 and clear t he white space so that line 1 and 3 had no gaps in the text file??? Thank you for your patience:

searchfile = open("servers.txt", 'r')
          connectionList = []           
          for line in searchfile:
              lineList = (line.strip()).split()
              connectionList.append(lineList)
              end = 0
          for x in connectionList:
              conns = int(x[3])
              end += conns
              mean = end /len(connectionList)
          print("Mean Value:",mean)   
lastNumbers = [] # List of all the last numbers. 
for line in searchfile: # Iterate through each line
    lineList = line.split(".", 50) # Split the line into everything past '.'
    lastNumber = lineList[len(lineList)-1] # Last aspect of the list.
    lastNumbers.append(lastNumber) # Add it to the list of last numbers.

I hope this is the answer you were looking for.

The Python standard library has two functions, min and max that get the minimum and maximum values in an array. But for learning purposes you probably want to see how to do it the hard way. You create two variables to keep track of the running min and max as you step through the array. To initialize these variables, start them = None and then assign them inside the loop. You can do everything in one loop, both computing the min, max, and mean.

searchfile = open("servers.txt", 'r')
connectionList = []           
for line in searchfile:
    lineList = (line.strip()).split()
    connectionList.append(lineList)
end = 0  # This line should be at the same indent level as `for`
x_min = None
x_max = None
for x in connectionList:
    conns = int(x[3])
    end += conns
    if x_min is None or conns < x_min:
        x_min = conns
    if x_max is None or conns > x_max:
        x_max = conns
mean = end /len(connectionList)
print("Mean Value:",mean)   
print("Max:", x_max)
print("Min:", x_min

The median is MUCH more difficult, and I suggest you take a look at some standard algorithms. The easiest way (but not the best way) is to put all the values in a list, sort the list, and take the middle element. But you aren't going to be able to do that without using library functions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM