所以,我有两个文件,所以为了检查其有效性,我执行了try
和except
两次。但我不认为这是一个好方法,你能建议一个更好的方法吗?
这是我的代码:
def form_density_dictionary(self,word_file,fp_exclude):
self.freq_dictionary={}
try:
with open(fp_exclude,'r')as fp2:
words_excluded=fp2.read().split() #words to be excluded stored in a list
print("**Read file successfully :" + fp_exclude + "**")
words_excluded=[words.lower() for words in words_excluded] # converted to lowercase
except IOError:
print("**Could not read file:", fp_exclude, " :Please check file name**")
sys.exit()
try:
with open(word_file,'r') as file:
print("**Read file successfully :" + word_file + "**")
words_list=file.read()
if not words_list:
print("**No data in file:",word_file +":**")
sys.exit()
words_list=words_list.split()
words_list=[words.lower() for words in words_list] # lowercasing entire list
unique_words=list((set(words_list)-set(words_excluded)))
self.freq_dictionary= {word:("%6.2f"%(float((words_list.count(word))/len(words_list))*100)) for word in unique_words}
#print((len(self.freq_dictionary)))
except IOError:
print("**Could not read file:", word_file, " :Please check file name**")
sys.exit()
还欢迎任何其他建议,使其更加pythonic。
答案 0 :(得分:1)
涉及文件系统路径的异常抛出具有filename
属性,可以像您一样使用显式属性word_file
和fp_exclude
。
这意味着您可以将这些IO操作包装在同一try-except
中并使用exception_instance.filename
,这将指示无法执行操作的文件。
例如:
try:
with open('unknown_file1.py') as f1, open('known_file.py') as f2:
f1.read()
f2.read()
except IOError as e:
print("No such file: {0.filename}".format(e))
最终打印出来:
No such file: unknown_file1.py
反过来说:
try:
with open('known_file.py') as f1, open('unknown_file2.py') as f2:
f1.read()
f2.read()
except IOError as e:
print("No such file: {0.filename}".format(e))
打印出来:
No such file: unknown_file2.py
答案 1 :(得分:1)
更多' pythonic'你可以使用来自馆藏库的callec Counter。
from collections import Counter
def form_density_dictionary(self, word_file, fp_exclude):
success_msg = '*Read file succesfully : {filename}'
fail_msg = '**Could not read file: {filename}: Please check filename'
empty_file_msg = '*No data in file :{filename}:**'
exclude_read = self._file_open(fp_exclude, success_msg, fail_msg, '')
exclude = Counter([word.lower() for word in exclude_read.split()])
word_file_read = self._file_open(word_file, success_msg, fail_msg, empty_file_msg)
words = Counter([word.lower() for word in word_file_read.split()])
unique_words = words - excluded
self.freq_dictionary = {word: '{.2f}'.format(count / len(unique_words))
for word, count in unique_words.items()}
如果你只想创建open_file方法,那就更好了,比如:
def _open_file(self, filename, success_msg, fails_msg, empty_file_msg):
try:
with open(filename, 'r') as file:
if success_msg:
print(success_msg.format(filename= filename))
data = file.read()
if empty_file_msg:
print(empty_file_msg.format(filename= filename))
return data
except IOError:
if fail_msg:
print(fail_msg.format(filename= filename))
sys.exit()
答案 2 :(得分:1)
跳出来的第一件事是缺乏一致性和可读性:在某些行中你缩进4个空格,而在其他行中你只使用两个;在某些地方,你在逗号后面放了一个空格,在其他地方你没有,在大多数地方你没有在赋值操作符周围有空格(=
)......
保持一致并使您的代码可读。最常用的格式是使用四个空格进行缩进,并且在逗号后总是有一个空格,但更重要的是保持一致,这意味着无论你选择什么,都要在整个代码中坚持使用它。它让每个人(包括你自己)都更容易阅读。
以下是我认为你可以改进的其他一些事情:
只有一个异常处理块而不是两个。
您也可以在一行中打开这两个文件。
更好的是,结合以前的建议并使用单独的方法从文件中读取数据,从而消除代码重复并使主要方法更易于阅读。
对于字符串格式设置,首选使用.format()
代替%
。看看这个:https://pyformat.info/
总体上尽量避免在代码中重复。如果您正在做多次,请将其提取到单独的函数或方法中,然后使用它。
这是你的代码很快被修改为我可能会写它的方式,并考虑到这些:
import sys
class AtifImam:
def __init__(self):
self.freq_dictionary = {}
def form_density_dictionary(self, word_file, exclude_file):
words_excluded = self.read_words_list(exclude_file)
words_excluded = self.lowercase(words_excluded)
words_list = self.read_words_list(word_file)
if len(words_list) == 0:
print("** No data in file: {} **".format(word_file))
sys.exit()
words_list = self.lowercase(words_list)
unique_words = list((set(words_list) - set(words_excluded)))
self.freq_dictionary = {
word: ("{:6.2f}".format(
float((words_list.count(word)) / len(words_list)) * 100))
for word in unique_words
}
@staticmethod
def read_words_list(file_name):
try:
with open(file_name, 'r') as file:
data = file.read()
print("** Read file successfully: {} **".format(file_name))
return data.split()
except IOError as e:
print("** Could not read file: {0.filename} **".format(e))
sys.exit()
@staticmethod
def lowercase(word_list):
return [word.lower() for word in word_list]