简体   繁体   中英

How to add numbers from a file into a list?

I am trying to read a file that has a list of numbers in each line. I want to take only the list of numbers and not the corresponding ID number and put it into a single list to later sort by frequencies in a dictionary.

I've tried to add the numbers into the list and I am able to get just the numbers that I need but I can not get it to add to the list correctly.

I have the function to read the file and to find just the location that I want to read from the line. I then try to add it to the list but it continues to come up like:

['23,43,56,', '67,87,34',]

And I want it to look like this:

[23, 43, 56, 67, 87, 34]

Here is my Code

def frequency():
f = open('Loto4.txt', "r")
list = []

    for line in f:
        line.strip('\n')
        start = line.find("[")
        end = line.find("]")
        line = line[start+1:end-1]
        list.append(line)
        print(line)

   print(list)
frequency()

This is the file that I am reading:

1:[36,37,38,9]

2:[3,5,28,25]

3:[10,14,15,9]

4:[23,9,31,41]

5:[5,2,21,9]

Try using a list comprehension on the line with append (i changed it to extend ), also please do not name variables a default python builtin, since list is one, I renamed it to l , but please do this on your own next time, also see @MichaelButscher's comment:

def frequency():
    f = open('Loto4.txt', "r")
    l = []

    for line in f:
        line = line.strip('\n')
        start = line.find("[")
        end = line.find("]")
        line = line[start + 1:end]
        l.extend([int(i) for i in line.split(',')])
        print(line)

    print(l)

frequency()

The literal_eval method of ast module can be used in this case.

from ast import literal_eval
def frequency()
    result_list = list()
    with open('Loto4.txt') as f:
        for line in f:
            result_list.extend(list(literal_eval(line)))
    print (result_list)
    return result_list

The literal_eval method of ast (abstract syntax tree) module is used to safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None. This can be used for safely evaluating strings containing Python values from untrusted sources without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.

def frequency():
    f = open('Loto4.txt', "r")
    retval = []

    for line in f:
        line.strip('\n')
        start = line.find("[")
        end = line.find("]")
        line = line[start+1:end-1]
        retval.extend([int(x) for x in line.split(',')])
        print(line)
    print(retval)

frequency()

I changed the name of the list to retval - since list is a builtin class.

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