2

What is the best way to read in a line of numbers from a file when they are presented in a format like this:

[1, 2, 3 , -4, 5]
[10, 11, -12, 13, 14 ]

Annoyingly, as I depicted, sometimes there are extra spaces between the numbers, sometimes not. I've attempted to use CSV to work around the commas, but the brackets and the random spaces are proving difficult to remove as well. Ideally I would append each number between the brackets as an int to a list, but of course the brackets are causing int() to fail.

I've already looked into similar solutions suggested with Removing unwanted characters from a string in Python and Python Read File, Look up a String and Remove Characters, but unfortunately I keep falling short when I try to combine everything.

Community
  • 1
  • 1
kommissarnicko
  • 23
  • 1
  • 1
  • 4
  • To solve the problem I ended up combining @Muhammad 's Method 2 and @Zac 's answer; `[int(s) for s in str.split() if s.isdigit()]` to clean up the string itself, then `eval()` to convert it into a list of ints for use by the rest of my program. – kommissarnicko Jan 22 '16 at 01:32

4 Answers4

4

Use regular expression to remove any unwanted characters from strings

import re
text_ = re.sub("[0-9]+", " ", text);

Second Method:

str = "h3110 23 cat 444.4 rabbit 11 2 dog"
>>> [int(s) for s in str.split() if s.isdigit()]
[23, 11, 2]
Muhammad Zeeshan
  • 470
  • 7
  • 24
  • The input is 'a line of numbers', it is *not* a line with random strings. –  Jan 20 '16 at 07:45
  • Then use regular expressions. You can learn more about expressions here and – Muhammad Zeeshan Jan 20 '16 at 07:47
  • I know regular expressions. But your answer does not answer the question which is about a line of integers written as a list. –  Jan 20 '16 at 07:49
  • If there are extra spaces that means the line is made of strings, even if the non numeric characters in it are just blanks. Regex is perfectly suited for this kind of operation. – arainone Jan 20 '16 at 07:51
  • So, method 2 did half of what I needed, and cleaned up the strings. However, I used `eval()` as suggested by @Zac to convert the whole string into a list of ints as I needed. I thought about using `ast`, but I really don't need to worry about malicious input since this is a school assignment. Good hustle to everyone regardless! – kommissarnicko Jan 22 '16 at 01:29
2

Using the ast.literal_eval() is another option:

from ast import literal_eval

with open("your_file.txt") as file_obj:
    for line in file_obj:
        lst = literal_eval(line)
        do_stuff(lst)
kylieCatt
  • 10,672
  • 5
  • 43
  • 51
1

Since each line already seems a literal python list you can use ast module:

import ast

with open('myfile.txt') as fh:
    for line in fh:
        numbers_list = ast.literal_eval(line)

Note that you could have obtained the same result using the builtin function eval() but using ast is more secure against malicious input.

Zac
  • 2,180
  • 2
  • 23
  • 36
0

Use the json module to parse each line as a JSON array.

import json

list_of_ints = []
for line in open("/tmp/so.txt").readlines():
    a = json.loads(line)
    list_of_ints.extend(a)
print(list_of_ints)

This collects all integers from all lines into list_of_ints. Output:

[1, 2, 3, -4, 5, 10, 11, -12, 13, 14]