python - Replacing one column in a file with other values -
i have file this:
1 0 1 0 1 0 2 0 2 0 2 0 3 0 3 0 3 0 3 0 3 0 4 0 5 0
and file, file2
1 column:
0.122 0.133 0.855 -2.1 -9.00
i want replace second column of file 1 data file2
such output file becomes this:
1 0.122 1 0.122 1 0.122 2 0.133 2 0.133 2 0.133 3 0.855 3 0.855 3 0.855 3 0.855 3 0.855 4 -2.1 5 -9
in other words, want copy data second file first until first column value remains same. when value in first column changes, picks second value other file.
i have been trying in python , have been able replace column not meet repetitions desired number of times. code copies data first 5 positions of column2 in first file , leaves rest. can suggest solution please?
it better see code might logic tweaked (it doesn't write original file , assumes second file has enough lines):
f1 = open('file1.txt', 'r') f2 = open('file2.txt', 'r') oldf1value = none v = none l1 in f1: [c1, c2] = l1.split() if (oldf1value != c1): oldf1value = c1 v = f2.readline().splitlines()[0] print(c1, v) f1.close() f2.close()
Comments
Post a Comment