简体   繁体   中英

Finding minimum in a column from .csv without using python module

I am creating a program that can help me find the minimum value in a column from a.csv file. However, before I can do this, I am struggling to sort the data into a list. Here is what I have done:

#open the file
csv_file = open("test_data.csv","r")
#print out all values in the last second column
for data in csv_file:
    data = data.replace("\n","").split(",")[-2]
    print(data)

This is what I get:

27
24
33
52
54
53

However, I would like to sort them in this way so I can use the min() function to find the minimum.

[27,24,33,52,54,53]

Any advice would be greatly appreciated thank you. :)

Why append to a list at all? Just get the minimum.

#open the file
csv_file = open("test_data.csv","r")
#print out all values in the last second column
results = []

for i, data in enumerate(csv_file):
    data = data.replace("\n","").split(",")[-2]
    if i == 0:
        minimum = data
    if data < minimum:
        minimum = data

print(minimum)

First create a list and then append the values.

#open the file
csv_file = open("test_data.csv","r")
#print out all values in the last second column
results = []
for data in csv_file:
    data = data.replace("\n","").split(",")[-2]
    print(data)
    results.append(data)
print(min(results))



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