Python“加入"功能类似于unix的"join"功能 [英] Python "join" function like unix "join"

查看:103
本文介绍了Python“加入"功能类似于unix的"join"功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇是否有内置的python连接功能,例如unix版本(请参见 http://linux.about.com/library/cmd/blcmdl_join.htm ).我知道该功能是通过内置的sqlite3模块以及pytables等其他模块提供的.

I am curious if there is a built-in python join function like the unix version (see http://linux.about.com/library/cmd/blcmdl_join.htm). I know the functionality is included through the built in sqlite3 module and probably through some other modules like pytables.

很抱歉,如果这是一个基本问题,但是我发现搜索"python join"和相关查询受到标准python join函数的相当污染.另外,如果没有这样的功能,我不会期望如此轻松地找到该信息.

Sorry if this is a basic question, but I'm finding that searching for "python join" and related queries is fairly polluted by the standard python join function. Also, if there is no such functionality, I would not expect to find that information so easily.

推荐答案

这是join函数的python版本,不能处理所有潜在的错误情况.但是展示了基本思想.

Here's a python version of the join function, does not handle all of the potential error cases. But demonstrates the basic idea.

# usage join(open('f1.txt'), open('f2.txt'))

def join(fd_a, fd_b) :
    result = []
    la = fd_a.readline()
    lb = fd_b.readline()
    while la and lb :
        start_a, rest_a = la.split(' ', 1)
        start_b, rest_b = lb.split(' ', 1)
        if cmp(start_a, start_b) == 0 :
            result.append([start_a, [rest_a, rest_b]])
            la = fd_a.readline()
            lb = fd_b.readline()
        elif cmp(start_a, start_b) < 0 :
            la = fd_a.readline()
        else :
            lb = fd_b.readline()
    return result

这篇关于Python“加入"功能类似于unix的"join"功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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