这是我的代码: 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?
非常感谢你。
答案 0 :(得分:6)
您的replacement
函数必须返回一个字符串,目前它返回None
。所以,改变声明
re.findall(pattern, content)
类似
return ' '.join(re.findall(pattern, content))
请注意,您不能只返回re.findall
的结果,因为这是一个列表,而不是字符串;你必须以某种方式列出该结果的清单。