Home > Software design >  reading two column file line by line
reading two column file line by line

Time:10-13

i have two column of file as arranged in the input file.I just want to read line by line and want to define the both items of each line to different variable.

For example while reading the file i need x = bht.sac0.SAC and y = 2017-01-01T01:01:00.000000Z.....so on

input file

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

i tried the code:

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

But it doesnot give what i expect,I hope experts may help me.Thanks in advance.

CodePudding user response:

Try:

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
  • Related