I am back again with another python query. I have been trying to do some calculations with the items present in a list. Here is the code:
import math
def Usage() :
print "Usage :python beznew.py transcriptionsFile"
if __name__ == "__main__" :
if len(sys.argv) != 2 :
Usage()
else :
transcriptionFile = sys.argv[1]
tFile = open(transcriptionFile, "r")
for line in iter(tFile) :
list = line.split()
# changing the unit of time from 100 nano seconds to seconds
list[0] = list[0] / 100000000
list[1] = list[1] / 100000000
# duration of each phoneme
list = list[1] - list[0]
# extracting the start time of each phoneme
newlist = list.pop[0]
print list
print newlist开发者_如何学运维
close.tFile
The input file looks like the following:
000000 1200000 pau
1200000 1600000 dh
1600000 2000000 ih
2000000 3100000 k
3100000 3400000 aa
3400000 3800000 r
I am trying to change the numerical values to seconds. And also trying to get the difference between first and second numbers. It would not allow me to divide. I dont understand what am I doing wrong. Thank you.
First, don't use list
as a variable name. Every time you do that, a kitten dies.
Second, you should convert the strings you've extracted from your file to a number, preferably a Decimal
if you value the precision. Currently you're trying to divide a string.
Third, nanoseconds are billionths of a second, not millionths.
Fourth, it's tFile.close()
, not close.tfile
.
Fifth, use for line in tfile:
. A file descriptor is already an iterator.
Sixth, use with open(transcriptionfile, "r") as tfile:
and be done with having to close it.
you can simplify your code as follows:
transcriptionFile = 'calculus.txt'
with open(transcriptionFile, "r") as tFile:
for line in tFile :
li = line.split()
if li:
new = ((int(li[1]) - int(li[0]))/10000000. , li[2])
print li,' ',new
The condition if li:
is here to eliminate possible void lines.
Important points:
don't call a list with the name
list
becauselist
is the name of built-in function of Pythonin Python,
10/100
produces 0 ; you must put a dot to obtain the right result:10./100
or10/100.
do the calculus
list = list[1] - list[0]
before dividing by 10000000, it is more precisewith open(....) as handle:
is better to open the files
Personally, I would do
transcriptionFile = 'calculus.txt'
with open(transcriptionFile, "r") as tFile:
gen = (line.split() for line in tFile if line.strip())
li = [((int(t2)-int(t1))/10000000.,phon) for (t1,t2,phon) in gen]
print '\n'.join(map(str,li))
Note that I used 10000000. to divide: if 1600000 - 1200000 = 400000
is in a unit which is 100 nanoseconds, then 400000 / 10000000
is 0.04 second
Edit 1
transcriptionFile = 'calculus.txt'
with open(transcriptionFile, "r") as tFile:
gen = (line.split() for line in tFile if line.strip())
firstVals, lapTimes = [],[]
for (t1,t2,phon) in gen:
firstVals.append( (int(t1)/10000000.,phon) )
lapTimes.append( (int(t2)-int(t1))/10000000.,phon) )
line.split()
returns a list of strings. Try list[0] = float(list[0]) / 100000000
.
This converts each string to a number which supports division before you do your calculations.
You do not convert the strings to numerical values. In order to conduct mathematical operations on your data, you have to convert the either to int
or float
objects:
valueA = int(list[0]) / 100000000
valueB = int(list[1]) / 100000000
精彩评论