简体   繁体   中英

How can I manipulate elements from text file in a list?

So I have made a code that extracts information from a text file. It contains temperatures of a given month, and its supposed to store it in a list. As I print it, it's correct. Furthermore I need to actually use these extracted numbers to calculate the mean, max and min etc... but it won't work. I get TypeError: unsupported operand type(s) for +: 'int' and 'list' when trying for example:

sum(oct_1945)/len(oct_1945)

also:

mean_1945=np.mean(oct_1945)

but I just get:

TypeError: unsupported operand type(s) for /: 'list' and 'int'

TypeError: unsupported operand type(s) for +: 'int' and 'list'

My full code and print statement is as following:

#xtract data for 1945 temperatures
def extract_data(temp_oct_1945):
    infile = open('temp_oct_1945.txt', 'r') #Open temps for 1945
    infile.readline() #skip first line
    oct_1945=[] #empty ist for temps
    for line in infile:
        numbers=line.split()
        oct_1945.append(numbers)
    infile.close()
    return oct_1945

oct_1945=extract_data('temp_oct_1945.txt')#define oct_1945


def extract_data(temp_oct_2014):
    infile = open('temp_oct_2014.txt', 'r') #Open temps for 2014
    infile.readline() #skip first line
    oct_2014=[] #empty ist for temps
    for line in infile:
        numbers=line.split()
        oct_2014.append(numbers)
    infile.close()
    return oct_2014

oct_2014=extract_data('temp_oct_2014.txt')#define oct_1945

#print statements
"""
1945 temperatures [['7.2', '8.1', '8.9', '11.6', '7.7', '8.7', '6.9'], 
                   ['5.4', '8.8', '8.9', '3.7', '3.3', '5.2', '9.6'], 
                   ['10.8', '5.0', '5.4', '9.5', '5.3', '5.8', '2.3'], 
                   ['4.1', '6.6', '8.2', '6.1', '8.9', '6.6', '4.1'], 
                   ['2.8', '2.1', '4.1']]
2014 temperatures [['9.8', '11.6', '11.5', '13.3', '12.6', '10.3', '7.5'], 
                   ['9.3', '10.3', '10.3', '8.4', '8.8', '5.0', '5.8'], 
                   ['6.8', '2.3', '3.5', '7.9', '11.8', '10.7', '9.0'], 
                   ['5.8', '6.8', '11.7', '10.6', '11.7', '13.1', '13.6'],
                   ['8.0', '3.5', '3.2']]
"""

Firstly, you have two functions that are essentially identical and share the same name. What we can do is have a single function that we pass the filename to. What we do is read the numbers as a list of strings per line, then join these together and map all entries of the list to floats and return the resulting list.

def extract_data(filename):
    infile = open(filename, 'r')  # open file
    infile.readline()  # skip first line
    numbers = []  # stores numbers as strings 
    for line in infile:
        numbers += line.split()
    infile.close()
    return list(map(float, numbers))
    
 print(extract_data('temp_oct_1945.txt'))
 print(extract_data('temp_oct_2014.txt'))

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