如何使用python获取.txt中括号之间的数据

如何使用python获取.txt中括号之间的数据,python,regex,Python,Regex,我的文本文件中有: ASABSNASN(900 200) 阿萨达达(500 600) 我需要知道如何提取括号中的两个数字,并将每个数字分配到X和Y,以便: x900 y200 x500 y600使用正则表达式: import re s = "asabsnabsn ( 900 200 ) asadadad ( 500 600 )" for i in re.findall(r"\((.*?)\)", s): X, Y = i.strip().split() print(X, Y

我的文本文件中有:

  • ASABSNASN(900 200)
  • 阿萨达达(500 600)
我需要知道如何提取括号中的两个数字,并将每个数字分配到
X
Y
,以便:


x900 y200 x500 y600
使用正则表达式:

import re
s = "asabsnabsn ( 900 200 ) asadadad ( 500 600 )"
for i in re.findall(r"\((.*?)\)", s):
    X, Y = i.strip().split()
    print(X, Y)
900 200
500 600
Ex:

import re
s = "asabsnabsn ( 900 200 ) asadadad ( 500 600 )"
for i in re.findall(r"\((.*?)\)", s):
    X, Y = i.strip().split()
    print(X, Y)
900 200
500 600
输出:

import re
s = "asabsnabsn ( 900 200 ) asadadad ( 500 600 )"
for i in re.findall(r"\((.*?)\)", s):
    X, Y = i.strip().split()
    print(X, Y)
900 200
500 600

假设您的文本文件如下所示

asabsnabsn ( 900 200 ) 
asadadad ( 500 600 )
然后是蟒蛇3

with open("datafile.txt") as textfile:
    for line in textfile:
        parts=line.split(' ')
        print("{} {}".format(parts[2],parts[3]))

您的问题表明,在试图获得编程帮助时,您的努力非常有限。你试过什么没用的?你的错误是什么?展示一个可复制的工作示例,你们通常会得到更好的答案。我想知道如何从txt文件中获得答案!!!这应该会有所帮助。