不同目录中相同文件的最后一列进行水平连接
创始人
2025-01-09 12:30:18
0

以下是一个示例代码,演示了如何实现“不同目录中相同文件的最后一列进行水平连接”的解决方法。假设我们有两个目录 dir1 和 dir2,它们包含相同文件名的文件,每个文件有多行数据,我们想要将每个文件的最后一列进行水平连接。

import os

def get_last_column(file_path):
    with open(file_path, 'r') as file:
        lines = file.readlines()
        last_column = [line.strip().split()[-1] for line in lines]
        return last_column

def horizontal_concatenation(dir1, dir2):
    # 获取两个目录下的所有文件名
    dir1_files = os.listdir(dir1)
    dir2_files = os.listdir(dir2)

    # 获取两个目录中相同文件名的文件路径
    common_files = set(dir1_files) & set(dir2_files)

    # 水平连接相同文件的最后一列
    for file_name in common_files:
        file1_path = os.path.join(dir1, file_name)
        file2_path = os.path.join(dir2, file_name)

        last_column1 = get_last_column(file1_path)
        last_column2 = get_last_column(file2_path)
        
        # 水平连接最后一列
        horizontal_concatenation = zip(last_column1, last_column2)

        # 打印结果
        print(f"Horizontal Concatenation of {file_name}:")
        for item in horizontal_concatenation:
            print(item)

# 示例使用
dir1 = './dir1'  # dir1 目录路径
dir2 = './dir2'  # dir2 目录路径
horizontal_concatenation(dir1, dir2)

请注意,上述代码仅用于演示目的,可能需要根据实际情况进行适当修改。

相关内容

热门资讯

透视讲解!wepoker好友助... 透视讲解!wepoker好友助力码,wepoker底牌透视(都是是真的有挂)1、每一步都需要思考,不...
透视ai代打!wepoker养... 透视ai代打!wepoker养号规律,wepoker有没有挂(果然是真的有挂)1、wepoker有没...
透视科技!wepoker辅助器... 透视科技!wepoker辅助器软件下载,wepoker游戏安装教程(一贯是真的有挂)1、下载好wep...
透视能赢!wepoker透视有... 透视能赢!wepoker透视有没有,wepoker俱乐部辅助器(果然有挂);1)wepoker俱乐部...
透视攻略!wepoker怎么下... 透视攻略!wepoker怎么下载游戏,wepoker怎么破解游戏(本来真的有挂)wepoker怎么破...
透视透视!wepoker轻量版... 透视透视!wepoker轻量版书签,wepoker有透视吗(原来是真的有挂)1、进入游戏-大厅左侧-...
透视中牌率!wepoker免费... 透视中牌率!wepoker免费脚本咨询,wepoker怎么提高运气(一直真的有挂)1、每一步都需要思...
透视辅助!wepoker可以免... 透视辅助!wepoker可以免费玩吗,wepokerplus作弊(一直存在有挂);1)wepoker...
透视最新!wepoker辅助真... 透视最新!wepoker辅助真的假的,wepoker黑侠辅助器(都是真的是有挂)1、打开软件启动之后...
透视了解!wepokerplu... 透视了解!wepokerplus到底是挂了吗,wepoker私人局俱乐部辅助(都是是真的有挂)1、w...