Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TypeError:应为字符串或缓冲区| Python_Python_Regex - Fatal编程技术网

TypeError:应为字符串或缓冲区| Python

TypeError:应为字符串或缓冲区| Python,python,regex,Python,Regex,我一直在尝试解析文本文件,并用正则表达式对其进行操作。 这是我的剧本: import re original_file = open('jokes.txt', 'r+') original_file.read() original_file = re.sub("\d+\. ", "", original_file) 如何修复以下错误: Traceback (most recent call last): File "filedisplay.py", line 4, in <module&

我一直在尝试解析文本文件,并用正则表达式对其进行操作。 这是我的剧本:

import re
original_file = open('jokes.txt', 'r+')
original_file.read()
original_file = re.sub("\d+\. ", "", original_file)
如何修复以下错误:

Traceback (most recent call last):
File "filedisplay.py", line 4, in <module>
original_file = re.sub("\d+\. ", "", original_file)
File "C:\Python32\lib\re.py", line 167, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or buffer
回溯(最近一次呼叫最后一次):
文件“filedisplay.py”,第4行,在
原始\u文件=re.sub(“\d+\”,“”,原始\u文件)
文件“C:\Python32\lib\re.py”,第167行,子文件
return\u compile(模式、标志).sub(repl、字符串、计数)
TypeError:应为字符串或缓冲区

为什么会出现此错误?

原始文件是一个文件对象,您需要使用它来获取其内容或正则表达式所需的缓冲区

通常,将
一起使用也很好(这样您就不必记得关闭文件了),因此您可能会得到如下结果:

import re

with open('jokes.txt', 'r+') as original_file:
    contents = original_file.read()
    new_contents = re.sub(r"\d+\. ", "", contents)

您将看到我在代码中绘制了正则表达式字符串(我在正则表达式字符串之前使用了
r
)。这也是一个很好的实践,因为有时您必须对某些字符进行双重转义,以使它们按照您的预期正常工作。

原始文件是一个文件对象,您需要使用它来获取其内容,或者正则表达式所需的缓冲区

通常,将
一起使用也很好(这样您就不必记得关闭文件了),因此您可能会得到如下结果:

import re

with open('jokes.txt', 'r+') as original_file:
    contents = original_file.read()
    new_contents = re.sub(r"\d+\. ", "", contents)

您将看到我在代码中绘制了正则表达式字符串(我在正则表达式字符串之前使用了
r
)。这也是一种很好的做法,因为有时您必须对某些字符进行双重转义,才能使它们按预期正常工作。

您调用
original\u file.read()
,但您不会将该值赋给任何对象

>>> original_file = open('test.txt', 'r+')
>>> original_file.read()
'Hello StackOverflow,\n\nThis is a test!\n\nRegards,\naj8uppal\n'
>>> print original_file
<open file 'test.txt', mode 'r+' at 0x1004bd250>
>>> 

我还建议将
与@Jerry一起使用,这样您就不必关闭文件来保存写入内容。

您可以调用
original\u file.read()
,但您不会将该值分配给任何内容

>>> original_file = open('test.txt', 'r+')
>>> original_file.read()
'Hello StackOverflow,\n\nThis is a test!\n\nRegards,\naj8uppal\n'
>>> print original_file
<open file 'test.txt', mode 'r+' at 0x1004bd250>
>>> 

我还建议将
与@Jerry一起使用
,这样您就不必关闭文件来保存写入内容。

原始文件是一个文件对象,您需要读取它来获取其内容或正则表达式所需的缓冲区。谢谢,我已经更新了代码,但它仍然会抛出一个错误:/Err,您没有将缓冲区放入变量
original\u file
,因此您仍然在正则表达式中使用file对象。为什么不使用另一个变量?像
contents=original\u file.read()
?解决了这个问题。。。(这里是新手!)
original\u file
是一个文件对象,您需要读取它以获取其内容,或者正则表达式所需的缓冲区。谢谢,我已经更新了代码,但它仍然抛出错误:/Err,您没有将缓冲区放入变量
original\u file
,因此您仍然在正则表达式中使用文件对象。为什么不使用另一个变量?像
contents=original\u file.read()
?解决了这个问题。。。(这里是新手!)这是解释性的。。。谢谢:)这是解释性的。。。谢谢:)