2015-02-04 103 views
-1
import re 

f= ('HelloHowAreYou') 
f = re.sub(r"([a-z\d])([A-Z])", r'\1 \2', f) 
# Makes the string space separated. You can use split to convert it to list 
f = f.split() 
print (f) 

这工作正常,以大写字母分隔所有的文本字符串,但是当我然后更改代码来阅读文本文件我有问题。任何人都可以解释一下为什么?阅读并删除文本文件

读我使用的文件:

f = open('words.txt','r') 

回答

1

读我使用的文件:

F =开放( 'words.txt', 'R')

但是,该代码不读取文件,它只会打开它。尝试:

my_file = open('words.txt','r') 
f = file.read() 
my_file.close() 

或者

with open('words.txt','r') as my_file: 
    f = my_file.read()