2015-02-04 102 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()