从文件中读取元组编号集

时间:2017-02-05 23:22:15

标签: python

我有一个前两行文件如下:

(1,233)(44,444)(2323,545)(0,0)(22,2)(5,0)(222,2)(0,5)(12,2)(22,3)
(0,223)(44,424)(123,545)(0,1)(42,2)(5,0)(52,2)(0,6)(17,4)(3,3)

我想阅读每个元组并获取每个数字(不带逗号和括号)。我读的是这样的:

file = filedialog.askopenfilename(filetypes=[("Text files","*.txt")])
if file is None:
    return False

with open(file, 'r') as f:
    lines = f.read().split("\n")
    firstLine = lines[0]
    secondLine = lines[1]

    # Run loop here to get the tuple values
    # firstNum = tuple[0]
    # secondNum = tuple[1]

    # Go to next line and do the same

我怎样才能做到以上几点?任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:2)

我会选择正则表达式:

[(int(x), int(y)) for x,y in re.findall(r"\((\d+),\s*(\d+)\)", line)]
# [(1, 233), (44, 444), (2323, 545), (0, 0), (22, 2), (5, 0), (222, 2), ...]

另一个有趣的解决方案是通过在对之间插入逗号将该行转换为有效的Python元组,然后应用ast.litereal_eval

list(ast.literal_eval(line.replace(")", "),")))
# [(1, 233), (44, 444), (2323, 545), (0, 0), (22, 2), (5, 0), (222, 2), ...]

答案 1 :(得分:2)

这是一种使用ast.literal_eval()

的相对简单的方法
import ast

with open('tuple.txt') as file:
    for line in file:
        numbers = ast.literal_eval(line.replace(')(', '),('))
        print('numbers: {}'.format(numbers))
        for _1st_num, _2nd_num in numbers:
            print(_1st_num, _2nd_num)

输出:

numbers: ((1, 233), (44, 444), (2323, 545), (0, 0), (22, 2), (5, 0), (222, 2), (0, 5), (12, 2), (22, 3))
1 233
44 444
2323 545
0 0
22 2
5 0
222 2
0 5
12 2
22 3
numbers: ((0, 223), (44, 424), (123, 545), (0, 1), (42, 2), (5, 0), (52, 2), (0, 6), (17, 4), (3, 3))
0 223
44 424
123 545
0 1
42 2
5 0
52 2
0 6
17 4
3 3

答案 2 :(得分:1)

第一种方式

用空格替换所有括号和逗号,然后拆分删除空格的行。因此,您获得所有数字的平面列表。通过迭代列表并获取索引ii+1的数字,您可以成对获取它们。

>>> line = "(1,233)(44,444)(2323,545)(0,0)(22,2)(5,0)(222,2)(0,5)(12,2)(22,3)"
>>> line_edited = line.replace("(", " ").replace(")", " ").replace(",", " ").split()
>>> line_edited
['1', '233', '44', '444', '2323', '545', '0', '0', '22', '2', '5', '0', '222', '2', '0', '5', '12', '2', '22', '3']
>>> for i in xrange(0, len(line_edited), 2):
    print line_edited[i], line_edited[i+1]


1 233
44 444
2323 545
0 0
22 2
5 0
222 2
0 5
12 2
22 3

第二种方式

与第一种方式类似,只是您只用空格替换括号并拆分。现在,您获得了一个字符串列表"first_number,second_number",因此您将遍历这些字符串并在逗号处拆分以获得该字符串。

>>> line_edited = line.replace("(", " ").replace(")", " ").split()
>>> line_edited
['1,233', '44,444', '2323,545', '0,0', '22,2', '5,0', '222,2', '0,5', '12,2', '22,3']
>>> for pair in line_edited:
    first_number, second_number = pair.split(",")
    print first_number, second_number


1 233
44 444
2323 545
0 0
22 2
5 0
222 2
0 5
12 2
22 3

您可以阅读有关替换字符串here中的多个字符的详细信息。

答案 3 :(得分:1)

你也可以这样做(我假设你的输入文件名为input_file):

data = (k.rstrip().replace("(", "").replace(")", " ").strip().split(" ") for k in open("input_file", 'r'))

for k in data:
    for j in (i.split(',') for i in k):
        print("{} {}".format(j[0], j[1]))

输出:

1 233
44 444
2323 545
0 0
22 2
5 0
222 2
0 5
12 2
22 3
0 223
44 424
123 545
0 1
42 2
5 0
52 2
0 6
17 4
3 3

答案 4 :(得分:0)

这样做 - 如果你能保持足够长的呕吐反射:

with open(file, 'r') as f:
    lines = f.read().split("\n")
    for line in lines:
      input=[scanf("%d,%d)",a) for a in line[1:].split('(')]
      # process each tuple in 'input' before continuing with next line

也许只是选择DYZ答案。

相关问题