Python:将文件中的2个数字提取到元组中

Python:将文件中的2个数字提取到元组中,python,string,format,tuples,extraction,Python,String,Format,Tuples,Extraction,我有一个数据文件,其格式如下: 1 3 1 1 85 3 1 88 3 2 55 3 2 92 3 2 11 3 2 12 1 我怎样才能得到前两个数字并将它们放入一个没有空格的元组中?第三个数字可以忽略 with open(foo) as f: answer = [tuple

我有一个数据文件,其格式如下:

   1       3        1
   1      85        3
   1      88        3
   2      55        3
   2      92        3
   2      11        3
   2      12        1
我怎样才能得到前两个数字并将它们放入一个没有空格的元组中?第三个数字可以忽略

with open(foo) as f:
    answer = [tuple(int(n) for n in line.split()[:2]) for line in f.readlines()]

其中
foo
是数据文件的路径。

类似这样的简单方法可能会奏效:

items = []
with open('numbers.txt') as f:
    for line in f:
        x, y, _ = map(int, line.strip().split())
        items.append((x, y))

print(items)
# [(1, 8), (1, 24), (1, 35), (1, 42), (1, 46), (1, 60), (1, 74)]
可以尝试以下方法:

with open('nums.txt', 'r') as f:
result = [tuple(map(int, l.split()[:2])) for l in f.readlines()]

print(result)
# returns list os tuples: [(1, 8), (1, 24), (1, 35), (1, 42), (1, 46), (1, 60), (1, 74)] 

当然,有很多方法可以做到这一点。 这是一个标准的数据争用操作,我建议使用和
dataframes
。如果尚未安装,请安装它

下面是一个示例代码,它假定您的数据文件名为
t.data
,并执行您想要执行的操作:

import pandas as pd
with open('t.data', 'rb') as datafile:
    df = pd.read_csv(datafile, sep='\s+',header=None)
# print data
subset_df = df[:][[0, 1]]
tuples = [tuple(x) for x in subset_df.values]
print tuples

# -- If you want to iterate --
# for index, row in subset_df.iterrows():
#     print row[0], row[1]
输出如下所示:

[(1, 8), (1, 24), (1, 35), (1, 42), (1, 46), (1, 60), (1, 74)]

尝试使用正则表达式
import re
数据文件的格式以及读取方式?如果你能分享你迄今为止所做的事情,也会有所帮助。欢迎来到堆栈溢出!请阅读,环顾四周,通读,特别是如果您遇到特定问题,请进行彻底的研究,在此处进行彻底的搜索,如果您仍然被困,请发布您的代码和问题的描述。另外,记住要包括。人们会乐意帮忙的