简体   繁体   中英

Reading comma separated tuples from a file in python

I am trying to read from a file with several tuples separated by commas. A sample input file looks like:

(0, 0), (0, 2), (0, 4), (-1, -1), (0, -2), (1, -1), (-1, -3),

(-1, 1), (-1, 3), (1, 1), (1, 3), (1, 5), (2, 0), (2, 2), (3, 3),

(2, 4), (3, 5), (4, 4), (5, 3), (6, 4), (5, 5), (7, 5)

After reading from this file, I need a tuple like this:

G = ((0, 0), (0, 2), (0, 4), (-1, -1), (0, -2), (1, -1), (-1, -3), \
(-1, 1), (-1, 3), (1, 1), (1, 3), (1, 5), (2, 0), (2, 2), (3, 3), \
(2, 4), (3, 5), (4, 4), (5, 3), (6, 4), (5, 5), (7, 5))

How this can be done efficiently? Regards.

Since they look like proper python tuples you can use literal_eval . Its fast as safe:

Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

import ast
s = '''(0, 0), (0, 2), ...'''
result = ast.literal_eval('({0})'.format(s))

Assuming there is a file.txt with the following content:

(0, 0), (0, 2), (0, 4), (-1, -1), (0, -2), (1, -1), (-1, -3)
(-1, 1), (-1, 3), (1, 1), (1, 3), (1, 5), (2, 0), (2, 2), (3, 3),
(2, 4), (3, 5), (4, 4), (5, 3), (6, 4), (5, 5), (7, 5)

You can use literal_eval() on each line in a loop and extend resulting list:

from ast import literal_eval

result = []
with open('file.txt', 'r') as f:
    for line in f:
        result.extend(literal_eval(line.strip()))

print result

prints:

[(0, 0), (0, 2), (0, 4), (-1, -1), (0, -2), (1, -1), (-1, -3), (-1, 1), (-1, 3), (1, 1), (1, 3), (1, 5), (2, 0), (2, 2), (3, 3), (2, 4), (3, 5), (4, 4), (5, 3), (6, 4), (5, 5), (7, 5)]

FYI, literal_eval() is safe:

Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

Hope that helps.

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