0

My question looks exactly like this post : Append float data at the end of each line in a text file

But for me, it is different. I have a dat file containing over 500 lines. I want that for each line, it adds me the value of the corresponding line in the second file. This second file only contains values like 0 or 1 in one column.

What I have :

File 1 :              File 2 :
1 2 3 4                 0
1 2 3 4                 1
1 2 3 4                 0

What I want :

File 1 :              File 2 :
1 2 3 4 0               0
1 2 3 4 1               1
1 2 3 4 0               0

What I've already tried :

Y = np.loadtxt('breastcancerY')
def get_number(_):
    lines = []
    for line in Y:
        print('this is a line', line)
        return " " + str(line) + '\n'

with open("breastcancerX","r") as f:
    data = f.read()

out = re.sub('\n',get_number,data)

with open("output.txt","w") as f:
    f.write(out)

When I do that and I print my values in file of 0 and 1, all the values are 0, it doesn't correspond to my file.

EDIT 1 :

Using this code :

# first read the two files into list of lines
with open("breastcancerY","r") as f:
    dataY = f.readlines()

with open("breastcancerX","r") as f:
    dataX = f.readlines()

# then combine lines from two files to one line.
with open("output.dat","w") as f:
    for X,Y in zip(dataX,dataY):
        f.write(f"{X} {Y}")

It gives me this

2 Answers2

1
# I don't understand what you want to do this this part
Y = np.loadtxt('breastcancerY')
def get_number(_):
    lines = []
    for line in Y:
        print('this is a line', line)
        return " " + str(line) + '\n'
# I don't understand what you want to do this this part



# first read the two files into list of lines
with open("breastcancerY","r") as f:
    dataY = f.readlines()

with open("breastcancerX","r") as f:
    dataX = f.readlines()

# then combine lines from two files to one line.
with open("output.txt","w") as f:
    for X,Y in zip(dataX,dataY):
        f.write(f"{X.strip()} {Y.strip()}\n")
fusion
  • 1,327
  • 6
  • 12
  • Thanks for your answer, but when I try this, it creates a new line corresponding to the Y file value But i don't want to create a new line, i want the Y value to be the last value of the line in X. When I open output file, it has created a new line – Kenichi-san Aug 31 '20 at 06:47
  • @Kenichi-san code updated. I forgot to strip the lines to remove the `\n` line breaks when writing to new file. – fusion Aug 31 '20 at 06:52
0

Using zip which provides the pairing of lines

Code

with open('file1.txt', 'r') as f1, open('file2.txt', 'r') as f2, open('fil3.txt', 'w') as f3:
  for line1, line2 in zip(f1, f2):
    f3.write(f'{line1.rstrip()} {line2}') # Writes:
                                          #   line from file1 without \n
                                          #   space, 
                                          #   corresponding line from file2

Files

File1.txt

1 2 3 4
1 2 3 4
1 2 3 4

file2.txt

0
1
0

Result: file3.txt

1 2 3 4 0
1 2 3 4 1
1 2 3 4 0
DarrylG
  • 16,732
  • 2
  • 17
  • 23