【问题标题】:reading two column file line by line逐行读取两列文件
【发布时间】:2021-10-11 18:09:42
【问题描述】:

我在输入文件中有两列文件。我只想逐行读取,并希望将每行的两项定义为不同的变量。

例如,在读取文件时,我需要 x = bht.sac0.SAC 和 y = 2017-01-01T01:01:00.000000Z.....等等

输入文件

bht.sac0.SAC    2017-01-01T01:01:00.000000Z
bht.sac1.SAC    2017-01-01T02:02:00.000000Z
bht.sac2.SAC    2017-01-01T03:03:00.000000Z
bht.sac3.SAC    2017-01-01T04:04:00.000000Z
bht.sac4.SAC    2017-01-01T05:05:00.000000Z
bht.sac5.SAC    2017-01-01T06:06:00.000000Z

我试过代码:

for x,y in inputfile:
    print(x)
    print(y)

但它并没有达到我的期望,我希望专家可以帮助我。提前谢谢。

【问题讨论】:

    标签: python for-loop split


    【解决方案1】:

    试试:

    with open("file.txt") as inputfile:
        for line in inputfile:
            x, y = line.split()
            print(x)
            print(y)
    
    bht.sac0.SAC
    2017-01-01T01:01:00.000000Z
    bht.sac1.SAC
    2017-01-01T02:02:00.000000Z
    bht.sac2.SAC
    2017-01-01T03:03:00.000000Z
    bht.sac3.SAC
    2017-01-01T04:04:00.000000Z
    bht.sac4.SAC
    2017-01-01T05:05:00.000000Z
    bht.sac5.SAC
    2017-01-01T06:06:00.000000Z
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 2014-06-21
      • 2012-06-13
      • 2016-11-22
      相关资源
      最近更新 更多