Python 读取和条带化文本文件

Python 读取和条带化文本文件,python,string,strip,Python,String,Strip,这可以很好地用大写字母分隔所有文本字符串,但是当我将代码更改为读取文本文件时,我遇到了问题。谁能解释一下为什么 要读取我正在使用的文件,请执行以下操作: 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

这可以很好地用大写字母分隔所有文本字符串,但是当我将代码更改为读取文本文件时,我遇到了问题。谁能解释一下为什么

要读取我正在使用的文件,请执行以下操作:

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=打开('words.txt','r')

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

f = open('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()