Absorance, And Transmittance Conversion In CSV
Hello I am trying to rewrite a CSV called TEST1.csv which is two columns, and I am trying to use this code except I don't understand how exactly to do it? import fileinput input_f
Solution 1:
Just replace the do_operation content with what you want to do to the value
This does what you specified in your example :
def do_operation(x):
return 10**( - ( float(x) - 2 ) )
input_file_name = str(input("Input file for absorbance/transmittance to be inserted? "))
output_file_name = str(input("What file name is it? "))
with open(output_file_name, 'w') as output_file:
with open(input_file_name) as input_file :
for line in input_file :
vals = line.split(',')
vals[1] = do_operation(vals[1])
output_file.write(','.join(vals)+'\n')
Edit : since you added an actual file ,and it contains floats you might want do a
x = float(x)
at the first line in do_operation
Solution 2:
Here's how to do it in-place:
import csv
import fileinput
import sys
#file_name = input("Input file for absorbance/transmittance to be inserted? ")
file_name = 'atc.csv' # Hardcoded for testing.
with fileinput.FileInput(files=file_name, inplace=True, backup='.sav') as csvfile:
for X, A in csv.reader(csvfile):
T = 10 ** -(float(A)-2)
print('{},{}'.format(X, T))
print('done', file=sys.stderr)
Solution 3:
I recently generated this file snippet which answers my question in a unique way:
from numpy import genfromtxt,concatenate,array,savetxt
mdarray= genfromtxt('C:\\DIR\\TEST1.csv',delimiter= ',', skip_header = 1)
percenttransmittance = array([10**(-(A-2)) for A in mdarray[:,1]]).reshape(len(mdarray[:,1]),1)
percenttransmittancearray= concatenate([mdarray[:,0].reshape(len(mdarray[:,1]),1),percenttransmittance], axis =1)
savetxt('C:\\DIR\\PercentTranmittance.csv',percenttransmittancearray,delimiter= ',')
Post a Comment for "Absorance, And Transmittance Conversion In CSV"