python TypeError: argument 1 must be string or read-only character buffer, not None
python TypeError: argument 1 must be string or read-only character buffer, not None
这是我的代码:
def main():
if len(sys.argv) != 3:
fname = "OP.tex"
dot_ind = fname.index(".") + 1
ofname = fname[:dot_ind] + "1." + fname[dot_ind:]
else:
fname = sys.argv[1]
ofname = sys.argv[2]
print (fname, "+",ofname)
writeout(ofname, replacement(readin(fname)))
def replacement(content):
pattern = re.compile(r'(?<=\\\[-16pt]\n)([\s\S]*?)(?=\\\n\thinhline)')
re.findall(pattern, content)
if __name__ == "__main__":
main()
为此我收到以下错误:
File "test.py", line 30, in main
writeout(ofname, replacement(readin(fname)))
File "/home/utilities.py", line 138, in writeout
out.write(content)
TypeError: argument 1 must be string or read-only character buffer, not None
writeout 是另一个文件中的一个函数,它告诉 python 在哪里输出内容,如下所示:
def writeout(filename, content, append=False):
"""
Writes content to file filename.
"""
mode = "w"
#append to the file instead of overwriting
if append:
mode = "a"
#write content
with open(filename, mode) as out:
out.write(content)
fname 和 ofname(输入和输出文件名)是正确的,为什么当它是一个字符串时程序说参数为 None?
非常感谢。
您的 replacement
函数必须 return 一个字符串,目前它 return 是 None
。所以,改变语句
re.findall(pattern, content)
类似于
return ' '.join(re.findall(pattern, content))
请注意,您不能只 return re.findall
的结果,因为那是一个列表,而不是字符串;您必须以某种方式从该结果中列出一份清单。
这是我的代码: def main():
if len(sys.argv) != 3:
fname = "OP.tex"
dot_ind = fname.index(".") + 1
ofname = fname[:dot_ind] + "1." + fname[dot_ind:]
else:
fname = sys.argv[1]
ofname = sys.argv[2]
print (fname, "+",ofname)
writeout(ofname, replacement(readin(fname)))
def replacement(content):
pattern = re.compile(r'(?<=\\\[-16pt]\n)([\s\S]*?)(?=\\\n\thinhline)')
re.findall(pattern, content)
if __name__ == "__main__":
main()
为此我收到以下错误:
File "test.py", line 30, in main
writeout(ofname, replacement(readin(fname)))
File "/home/utilities.py", line 138, in writeout
out.write(content)
TypeError: argument 1 must be string or read-only character buffer, not None
writeout 是另一个文件中的一个函数,它告诉 python 在哪里输出内容,如下所示:
def writeout(filename, content, append=False):
"""
Writes content to file filename.
"""
mode = "w"
#append to the file instead of overwriting
if append:
mode = "a"
#write content
with open(filename, mode) as out:
out.write(content)
fname 和 ofname(输入和输出文件名)是正确的,为什么当它是一个字符串时程序说参数为 None?
非常感谢。
您的 replacement
函数必须 return 一个字符串,目前它 return 是 None
。所以,改变语句
re.findall(pattern, content)
类似于
return ' '.join(re.findall(pattern, content))
请注意,您不能只 return re.findall
的结果,因为那是一个列表,而不是字符串;您必须以某种方式从该结果中列出一份清单。