2016-09-06 30 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 
相關問題