在 Python 中搜索包含制表符分隔值的 TXT 文件 [英] Search in a TXT file containing tab-separated values in Python

查看:52
本文介绍了在 Python 中搜索包含制表符分隔值的 TXT 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TXT 文件,其中包含以下形式的制表符分隔值(3 列):

I have a TXT file which consists of tab-separated values of the following form (3 columns):

type        color  name
fruit       red    apple
fruit       red    grape
vegetable   green  cucumber

我正在寻找一种方法来读取该文件并在该数据集中执行选择",就像我在 SQL 表中选择一样(类似这样)

I'm looking for a way to read that file and perform a "select" in that data set as I would be selecting in an SQL table (something like this)

SELECT name FROM data_set WHERE color='red' and type='fruit'

我知道应该像这样读取和迭代文件:

I understand that the file should be read and iterated through like so:

f = open('file_name.txt', 'r')
for line in f:

但是,我不确定在给定前两列的情况下返回第三列的值的查找部分的最有效方法是什么.

However I'm not sure of what would be the most efficient way of the lookup part which would return the value of the third column given the first two.

我使用的是 Python 2.7.任何帮助将不胜感激!

I'm using Python 2.7. Any help would be greatly appreciated!

推荐答案

这里有一种方法可以让你做类似的事情.这不完全是您想要的,但它有效:

Here is a way how you can do something like it. It is not exactly what you want, but it works:

data = {}

with open('file_name.txt', 'r') as f:
    lines = [line.rstrip() for line in f]
    lines.pop(0)

for line in lines:
    x = line.split('\t')
    try:
        data[x[0]][x[1]].append(x[2])
    except KeyError:
        data.update({x[0]: {x[1]: []}})
        data[x[0]][x[1]].append(x[2])

print(data['fruit']['red'])
-> ['apple', 'grape']

print(data['vegetable']['green'])
-> ['cucumber']

这篇关于在 Python 中搜索包含制表符分隔值的 TXT 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆