這裏是一個在python腳本中使用的Unicode單詞數組。將單詞從文件讀入集合,維護順序
texts =[u"abc", u"pqr", u"mnp"]
該腳本按照預期與上述3個單詞的例子一起工作。問題在於文本文件中有數千個單詞。 如何從文本文件讀取?
更新: 我有2個問題。輸出中不會保留文本文件中的單詞序列。 該文本文件具有unicode字符,因此在我的原始示例中爲「u」。
# cat testfile.txt
Testing this file with Python
# cat test.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
f = open('testfile.txt', 'r')
texts = set(f.read().split())
print (texts)
# python test.py
set(['this', 'Python', 'Testing', 'with', 'file'])
重複:http://stackoverflow.com/questions/4963499/ – Wok 2011-05-07 07:12:31