Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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
Python html2text添加随机\n_Python_Python 2.7 - Fatal编程技术网

Python html2text添加随机\n

Python html2text添加随机\n,python,python-2.7,Python,Python 2.7,使用python包将html转换为标记时,会在文本中添加“\n”。我在上尝试演示时也看到了这种行为 有没有办法关掉这个?当然,我可以自己删除它们,但在原始文本中可能会出现我不想删除的“\n” html2text('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad min

使用python包将html转换为标记时,会在文本中添加“\n”。我在上尝试演示时也看到了这种行为

有没有办法关掉这个?当然,我可以自己删除它们,但在原始文本中可能会出现我不想删除的“\n”

    html2text('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.')

    u'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\ntempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,\nquis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo\nconsequat. Duis aute irure dolor in reprehenderit in voluptate velit esse\ncillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non\nproident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n\n'
现在,您可以通过将
BODY\u WIDTH
设置为
0
来禁用包装行为。大概是这样的:

import html2text
html2text.BODY_WIDTH = 0
text = html2text.html2text('...')

当然,重置
BODY\u WIDTH
会全局更改模块的行为。如果我需要访问此功能,我可能会尝试修补模块,为
html2text()
创建一个参数来修改每次调用的行为,并将此修补程序提供给作者。

在最新版本的html2text中,请执行以下操作:

import html2text
h = html2text.HTML2Text()
h.body_width = 0
note = h.handle("<p>Hello, <a href='http://earth.google.com/'>world</a>!")
导入html2text
h=html2text.html2text()
h、 车身宽度=0
注意=h.handle(“Hello,!”)

这将删除html2text否则所做的单词包装

谢谢,这很有效!我正在寻找函数的一个参数,在模块级修改这个参数似乎很难看。最新版本似乎将它作为HTML2Text类的一个属性。构造完成后,您可以说
您的h2t.body_width=0
。也就是说,我还没有测试过它。@AndréChristofferAndersen(或任何其他人):如果您测试过,请随时建议对我的答案进行编辑。当前维护的脚本版本在此版本,对上一版本无效,下面@christoffer给出的答案确实有效,但是请注意,
body\u width
必须为0,而不是零才能起作用!您还可以将其作为
html2text.html2text(html\u字符串,bodywidth=0)传递