I have a .dat file. I want to read each line and replace a value in each line with a different value. Then save this to a new out.dat file. Every replacement is independent and I want to copy over the first 25 lines before starting the replacement. I am also doing this multiple times to create many different out files, hence the “for k in chan” in the beginning of my attempt.
My attempt was to open the in.file, then use enumerate to read the lines and index them. By indexing, I could jump to the 25th line to begin replacements. I used np.loadtxt to open in.dat and create a list of the values I want to replace – this is what data1 is. Then, data2 is a randomized list of values. So I was hoping that I could begin at line 25, find the first value of data1, replace it with second value of data2, then go to line 26, find the second value of data1 and replace it with second value of data2 and so on. Instead, the code I typed finds the first value of data1 in every line and replaces it with the first value of data2. It doesn’t loop through all the values while reading the lines.
Data1 = [0, 0.23, 0.26, 9.27, 10…..]
Data2 = [20, 29, 27, 15, 16….]
So overall I want to read a line, find string 0 replace with string 20, then go to next line, find string 0.23 replace with 29 and so on.
Maybe there is a way to do this without a for loop. Is it possible to load a column from in.dat and replace it with another column of data? Unfortunately, when I load in.dat it creates a 1d array so there are no column headers to locate and replace doing it that way.
for k in chan:
with open('in.dat', 'rt') as fin, open(f'out{k}.dat', 'w') as fout:
for i,line in enumerate(fin, 1):
if i > 25:
for l, j in zip(data1, data2)
modified = line.replace(str(l), str(j), 1)
fout.write(modified)
else:
fout.write(line)
Allyand Camshow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I’d suggest testing with a single file – the first value of chan, whatever that is. Also, I’d suggest enumerating from 0, as below, but if you start from one, as per your example, you need if i>24:
to start at row 25.
Since you already have the index from enumerate
you can use that for data1 and data2, just use i-24
with open('in.dat', 'rt') as fin, open(f'out.dat', 'w') as fout:
for i,line in enumerate(fin):
if i > 23:
l = data1[i-24]
j = data2[i-24]
modified = line.replace(str(l), str(j), 1)
fout.write(modified)
else:
fout.write(line)
You can compress this example to less lines, but I thought this was clearer.
Also, take care that your data1 and data2 lengths are sufficient or you will get IndexError: list index out of range