2016-09-06 72 views
1

我目前正试图过滤词库/词典,使其只包含我需要的词。字典中有两列是第一个字,第二个字是发音(见下图)。如何使第1列和第2列之间的空间相同

Snippet of lexicon

词汇可用here

有没有什么办法可以让这个空间/分隔符适用于所有的情况......它会让事情变得更容易。

+2

该空间是一个选项卡,所以可以伊斯利do'for在文件行:值= line.split ('\ t')',然后使用'values [0]'来存取单词,并使用'values [1]' –

+0

来拼音。“我感到很蠢。它解决了我的问题。非常感谢 –

回答

0

你的意思是类似于以下内容? here

在这种情况下,这是代码(不使用任何特定的字符):

#!/usr/bin/env python2 

import sys 

path_to_the_file = sys.argv[1] 

word = [] 
pron = [] 
maxword = 0 
with open(path_to_the_file) as fr: 
    for line in fr: 
     words = line.split() 
     word.append(words[0]) 
     pron.append(' '.join(words[1:])) 
     if len(words[0]) > maxword: maxword = len(words[0]) 

format_str = '{:'+str(maxword)+'s} {:s}\n' 

msg = '' 
for w,p in zip(word,pron): 
    msg += format_str.format(w,p) 

print msg 
相关问题