如何在一行

时间:2016-07-04 22:33:31

标签: python

我在这方面有点新手,所以请光临......我已经为我的工作编写了一个程序来返回文件名和持续时间,以便我可以轻松地将它们添加到电子表格中。我希望在同一行返回类似的文件名,这样我就可以一次复制整行相同的文件。

到目前为止,这是我的代码:

import os, wave

def main():

    directory = input("What is the directory? ")
    for file in sorted(os.listdir(directory)):
        if file.endswith(".wav"):
            with wave.open(file, 'rb') as f:
                frames = f.getnframes()
                rate = f.getframerate()
                total_seconds = frames / float(rate)
                minutes = total_seconds / 60
                seconds = total_seconds % 60
                hours = minutes / 60
                minutes = minutes % 60
                duration = '%.2d:%.2d:%.2d' % (hours, minutes, seconds)
                print(file[:-4], duration)

main()

它返回:

sa-01003-32-su01-pm 00:04:41
sa-01003-32-su02-pm 00:02:20
sa-01003-32-su03-pm 00:09:50
sa-01003-32-su05-pm 00:09:41
sa-01003-32-su06-pm 00:10:15
sa-01003-32-su07-pm 00:09:49
sa-01003-32-su08-pm 00:01:05
sa-01003-33-su01-pm 00:04:04
sa-01003-33-su03-pm 00:02:09
sa-01003-33-su04-pm 00:09:45
sa-01003-33-su05-pm 00:09:39
sa-01003-33-su06-pm 00:10:30
sa-01003-33-su07-pm 00:09:56
sa-01003-33-su08-pm 00:01:05

所以我希望所有的' 32'文件和持续时间在一行中,仅由空格分隔,并且' 33'下一行的文件和持续时间等。

3 个答案:

答案 0 :(得分:1)

Alter TABLE TABLE_NAME_HERE
CHARACTER SET utf8 COLLATE utf8_unicode_ci;

它没有经过测试,但应该可以使用。

请记住,该脚本仅适用于包含以短划线分隔的值的文件名。

答案 1 :(得分:1)

只需将此结果放在字符串变量中,而不是在主函数末尾打印所需的信息:

import os, wave

def main():
    directory = input("What is the directory? ")

    current = None
    similar = []

    for file in sorted(os.listdir(directory)):
        if file.endswith(".wav"):
            with wave.open(file, 'rb') as f:
                frames = f.getnframes()
                rate = f.getframerate()
            total_seconds = frames / float(rate)
            minutes = total_seconds / 60
            seconds = total_seconds % 60
            hours = minutes / 60
            minutes = minutes % 60
            duration = '%.2d:%.2d:%.2d' % (hours, minutes, seconds)

            txt = file[:-4] + " " + duration
            value = file.split("-")[3]
            if not current == None:
                if value == current:
                    similar.append(txt)
                else:
                    print(" ".join(similar))
                    similar = []
            else:
                current = value
                similar.append(txt)

main()

而不是打印:

results= ''

results += file[:-4] + ' ' + duration + '\n' 用于返回。

答案 2 :(得分:0)

为了回答你的问题,我创建了一个字典(包含文件名及其相应的持续时间)和一个列表(包含文件名):它们都由main()函数返回。接下来,我创建了一个根据您的需要打印值的函数。简而言之,该函数检查列表中的名称是否具有相同的前缀(例如'sa-01003-32-):如果是,则名称及其相应的持续时间(来自字典)打印在由a分隔的同一行上空间。如果不是,则打印新行。这是代码:

def main():
    directory = input("What is the directory? ")
    dic_file={} # A dictionary that will store the file names and their duration
    list_file=[] # A list of the file names (needs to contain the same names that are present in the dictionary)
    for file in sorted(os.listdir(directory)):
        if file.endswith(".wav"):
            with wave.open(file, 'rb') as f:
                frames = f.getnframes()
                rate = f.getframerate()
                total_seconds = frames / float(rate)
                minutes = total_seconds / 60
                seconds = total_seconds % 60
                hours = minutes / 60
                minutes = minutes % 60
                duration = '%.2d:%.2d:%.2d' % (hours, minutes, seconds)
                list_file.append(file)
                dic_file.setdefault(file,duration)
    return dic_file,list_file  #return the dictionary and the list

def print_value(dic_file,list_file):
    list_file.sort()
    for n,item in enumerate(list_file):
        while True:
            if n<len(list_file)-1:
                if list_file[n+1][:-11]==item[:-11]:
                    print(item,dic_file[item],end=' ')
                else:
                    print(item,dic_file[item]+'\n')
            break
    print(list_file[len(list_file)-1],dic_file[list_file[len(list_file)-1]])

dic_file,list_file=main()
print_value(dic_file,list_file)