【问题标题】:Trying to join a .dat file with a .asc file using Python or Excel尝试使用 Python 或 Excel 将 .dat 文件与 .asc 文件连接起来
【发布时间】:2017-09-28 03:13:26
【问题描述】:

伙计们。

我在尝试将两个大数据文件合并在一起时遇到了一些独特的问题。两个文件都有一列相同的数据(专利号),而所有其他列都不同。

想法是将它们连接起来,使这些专利号列对齐,以便其他数据可读和连接。

.dat 文件的前几行如下所示:

IL      1   Chicago 10030271    0   3930271
PA      1   Bedford 10156902    0   3930272
MO      1   St. Louis   10112031    0   3930273
IL      1   Chicago 10030276    0   3930276

还有.asc

02 US corporation   No change   11151713    TRANSCO PROD INC    58419
02 US corporation   No change   11151720    SECURE TELECOM INC  502530 
02 US corporation   No change   11151725    SOA SYSTEMS INC 520365 
02 US corporation   No change   11151738    REVTEK INC  473150 

.dat 文件太大而无法在 Excel 中完全打开,所以我认为没有办法重新组织它(我不知道它是否是通过我在网上找到的任何宏)。

我觉得这是一个新手问题,但有谁知道我如何将这些数据集与这个专利号唯一标识符链接在一起(最好使用 Python)?

【问题讨论】:

    标签: python excel parsing merge uniqueidentifier


    【解决方案1】:

    您需要编写一个程序,从您想要合并的两个文件中读取数据。您将打开文件并解析每一行的数据。从那里您可以按照您想要的任何顺序将数据写入新文件。这可以通过 python 文件 IO 完成。

    伪代码:

    def filehandler(self, filename1, filename2):
         Fd =open(filename1, "r")
         Fd2 = open(filename2, "r")
         while True:
             line1 = Fd.readline()
             if not line1: break # this will exit the loop if there is no more to read
             Line1_array = line1.split()
             # first line of first file is split and saved in an array deliniated by spaces.
    

    【讨论】:

    • 很公平,但是如果两个数据集的长度不相等,这会起作用吗?我相信一个条目比另一个条目多得多,因此我需要使用唯一标识符来匹配它们。
    • 当然可以。在它们中添加一个 If 语句以检查是否还有另一行。这与您跳出 while 循环的方式相同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 2017-05-07
    • 1970-01-01
    • 2014-12-13
    • 2016-04-07
    • 2021-12-01
    • 2020-09-07
    相关资源
    最近更新 更多