Reputation: 471
I have a input data in a file input.txt. I want to add 200 with each row of 4th column and want to append just beside to it in the format same as original.
Input.txt
> > >
0.000 5.064 -0.15835E-01 -7.755619582136471027e-03
0.700 9.064 -0.21715E-02 -1.302134466433525067e-02
0.800 5.064 0.18954E-01 -1.691809184964498253e-02
> > >
0.500 4.977 0.32899E-02 1.439701445411046415e-02
0.200 6.977 0.15972E-01 6.094966468334198084e-03
0.700 4.977 0.30631E-01 -5.640445453166963163e-03
out.txt
> > >
0.000 5.064 -0.15835E-01 -7.755619582136471027e-03 199.99224438041787
0.700 9.064 -0.21715E-02 -1.302134466433525067e-02 199.98697865533566
0.800 5.064 0.18954E-01 -1.691809184964498253e-02 199.98308190815035
> > >
0.500 4.977 0.32899E-02 1.439701445411046415e-02 200.01439701445412
0.200 6.977 0.15972E-01 6.094966468334198084e-03 200.00609496646834
0.700 4.977 0.30631E-01 -5.640445453166963163e-03 199.99435955454683
I tried :
import numpy as np
data=np.loadtxt('input.txt')
data1=data(:,3)+200
I am getting error ValueError: could not convert string to float: '>'
I hope some expert will help me overcoming this problem.
Upvotes: 0
Views: 279
Reputation: 46
We can achieve this without creating a new file and do the operation inplace using fileinput library. In the blow code it is preserving this ">" character too.
import sys
import fileinput
file = fileinput.input('test.txt', inplace=1)
for i, line in enumerate(file):
if line.startswith('>'):
sys.stdout.write(line)
else:
string = line.split()
string.append('200\n')
line = ' '.join(string)
sys.stdout.write(line)
file.close()
output of above code:
> > >
0.700 4.977 0.30631E-01 -5.640445453166963163e-03 200
0.700 4.977 0.30631E-01 -5.640445453166963163e-03 200
0.700 4.977 0.30631E-01 -5.640445453166963163e-03 200
> > >
0.700 4.977 0.30631E-01 -5.640445453166963163e-03 200
0.700 4.977 0.30631E-01 -5.640445453166963163e-03 200
0.700 4.977 0.30631E-01 -5.640445453166963163e-03 200
Upvotes: 0
Reputation: 57033
You have non-numeric symbols in the file. You can treat them as comments:
data = np.loadtxt('input.txt', comments=">")
But then you will not be able to preserve them in the output file. I am afraid your only option is to read the file line by line in a loop:
CHANGE = 200
with open('input.txt') as infile, open('out.txt', 'w') as outfile:
for line in infile:
if not line.startswith('>'): # Needs a modification
parts = line.split()
parts.append(str(float(parts[3]) + CHANGE) + '\n')
line = ' '.join(parts)
outfile.write(line)
Upvotes: 1
Reputation: 66
I dont quite understand the question but I think I know what the problem is. There are a couple ways I may be able to help you:
>
s at the beginning of the file.>
s. Like this:with open("input.txt", "r") as f:
with open("temporaryfile.txt", "w") as temp:
temp.write(f.read().replace("> > >",""))
data=np.loadtxt("temporaryfile.txt")
data1=data(:,3)+200
and just use import os;os.remove("temporaryfile.txt")
to remove temporaryfile.txt
afterward.
The problem is its running into those >
s and that is what is causing the error. So, if you remove those, that should fix your problem.
Upvotes: 0