我有两个具有相同列数但行数不同的文件。一个文件是时间戳列表和单词列表,第二个文件是时间戳列表以及每个单词中的声音列表,即:
9640 12783 she
12783 17103 had
...
和:
9640 11240 sh
11240 12783 iy
12783 14078 hv
14078 16157 ae
16157 16880 dcl
16880 17103 d
...
我想合并这两个文件并创建一个条目列表,其中单词作为一个值,音标作为另一个值,即:
[['she', 'sh iy']
['had', 'hv ae dcl d']
...
我是一个完整的 Python(和编程)菜鸟,但我最初的想法是通过在第二个文件中搜索第一个文件中的第二个字段,然后将它们附加到一个列表中来做到这一点。我试过这样做:
word = open('SA1.WRD','r')
phone = open('SA1.PHN','r')
word_phone = []
for line in word.readlines():
words = line.split()
word = words[2]
word_phone.append(word)
for line in phone.readlines():
phones = line.split()
phone = phones[2]
if int(phones[1]) <= int(words[1]):
word_phone.append(phone)
print word_phone
这是输出:
['she', 'had', 'your', 'dark', 'suit', 'in', 'greasy', 'wash', 'water', 'all', 'year', 'sh', 'iy', 'hv', 'ae', 'dcl', 'd', 'y', 'er', 'dcl', 'd', 'aa', 'r', 'kcl', 'k', 's', 'uw', 'dx', 'ih', 'ng', 'gcl', 'g', 'r', 'iy', 's', 'iy', 'w', 'aa', 'sh', 'epi', 'w', 'aa', 'dx', 'er', 'q', 'ao', 'l', 'y', 'iy', 'axr']
正如我所说,我是个菜鸟,一些建议会很有帮助。
更新: 如果可能的话,我想重新讨论这个问题。我已经修改了 Lattyware 的代码以对目录进行操作:
phns = []
wrds = []
for root, dir, files in os.walk(sys.argv[1]):
wrds = wrds + [ os.path.join( root, f ) for f in files if f.endswith( '.WRD' ) ]
phns = phns + [ os.path.join( root, f ) for f in files if f.endswith( '.PHN' ) ]
phns.sort()
wrds.sort()
files = (zip(wrds,phns))
#OPEN THE WORD AND PHONE FILES, COMPARE THEM
output = []
for file in files:
with open( file[0] ) as unsplit_words, open( file[1] ) as unsplit_sounds:
sounds = (line.split() for line in unsplit_sounds)
words = (line.split() for line in unsplit_words)
output = output + [
(word, " ".join(sound for _, _, sound in
takeuntil(sounds, stop)))
for start, stop, word in words
]
我想在这些文件的文件路径中保留一些信息。我想知道如何将拆分文件路径附加到此代码返回的列表中的元组中,例如,
[('she', 'sh iy', 'directory', 'subdirectory'), ('had', 'hv ae dcl d', 'directory', subdirectory')]
我想我可以拆分路径,然后将列表压缩在一起,但是上面输出的代码列表中有 53,000 个项目,但只有 6300 个文件对正在处理。