使用列表中的项目进行数值计算

zin*_*ngy 0 python

我又回来了另一个python查询.我一直试图用列表中的项目进行一些计算.这是代码:

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 
Run Code Online (Sandbox Code Playgroud)

输入文件如下所示:

000000 1200000 pau

1200000 1600000 dh

1600000 2000000 ih

2000000 3100000 k

3100000 3400000 aa

3400000 3800000 r
Run Code Online (Sandbox Code Playgroud)

我试图将数值更改为秒.并且还试图获得第一个和第二个数字之间的差异.它不允许我划分.我不明白我做错了什么.谢谢.

Tim*_*ker 5

首先,不要list用作变量名.每当你这样做,一只小猫就会死去.

其次,您应该将从文件中提取的字符串转换为数字,Decimal如果您重视精度,则最好将其转换为数字.目前你正试图划分一个字符串.

第三,纳秒是十亿分之一秒,而不是百万分之一秒.

第四tFile.close(),不是close.tfile.

五,使用for line in tfile:.文件描述符已经是迭代器.

第六,使用with open(transcriptionfile, "r") as tfile:并完成必须关闭它.