TypeError:强制转换为Unicode,需要字符串或缓冲区,找不到NoneType [英] TypeError: coercing to Unicode, need string or buffer, NoneType found
问题描述
当前为程序和一个组件编写函数是要搜索python文件中是否正在使用单个变量.
Currently writing a function for a program and one component is to search whether a single variables are being used within a python file.
功能:
def SINGLE_CHAR_VAR(python_filename):
file = open(python_filename)
lines = [0]
SINGLE_CHAR_VAR = []
for line in file:
stripped = line.strip('\n\r')
lines.append(stripped)
from utils import vars_indents
variable_list = (vars_indents(python_filename))[0]
for i in range(1, len(variable_list)):
if len(variable_list[i][0][0]) == 1:
SINGLE_CHAR_VAR.append(['SINGLE_CHAR_VAR', i, variable_list[i][0][1], variable_list[i][0][0], lines[i]])
return SINGLE_CHAR_VAR
当我单独使用该功能时-该功能正常运行.但是,当我整体调用该程序时-我收到以下错误消息:
When i used the function by itself - the function works properly. However when i call upon the program as a whole - i get the following error message:
Traceback (most recent call last):
File "<web session>", line 1, in <module>
File "lint_2.py", line 141, in lint
sorted_error_list = sorted_list(list_of_file_errors)
File "lint_2.py", line 84, in sorted_list
error_list = total_error_list(python_filename)
File "lint_2.py", line 65, in total_error_list
single_char_var_list = SINGLE_CHAR_VAR(python_filename)
File "lint_2.py", line 33, in SINGLE_CHAR_VAR
file = open(python_filename)
TypeError: coercing to Unicode: need string or buffer, NoneType found
我绝对不知道-我要去哪里了-任何帮助都是非常非常珍惜的!!!
I have absolutely no idea - where I am going wrong - any help would be very, very, very cherished!!!
谢谢.
推荐答案
python_filename
设置为 None
,这对于 open()
函数:
python_filename
is set to None
, which is not a valid argument for the open()
function:
>>> open(None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, NoneType found
为什么 python_filename
为 None
不能从您发布的代码中确定.您提供的回溯表明该值起源于 sorted_list()
函数,我建议您开始寻找线索:
Why python_filename
is None
cannot be determined from the code you posted. The traceback you supplied suggests the value originates in the sorted_list()
function, I suggest you start looking there for clues:
File "lint_2.py", line 84, in sorted_list
error_list = total_error_list(python_filename)
但是,这只是一个猜测;您将不得不追溯该追溯中的所有代码,以查看究竟是什么地方首次设置了 None
.
However, that is just a guess; you'll have to trace through all the code in that traceback to see where exactly the None
is first set.
这篇关于TypeError:强制转换为Unicode,需要字符串或缓冲区,找不到NoneType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!