Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.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 从美化组替换\n\t_Python_Replace_Beautifulsoup_Special Characters - Fatal编程技术网

Python 从美化组替换\n\t

Python 从美化组替换\n\t,python,replace,beautifulsoup,special-characters,Python,Replace,Beautifulsoup,Special Characters,您好,我正在使用BeautifulSoup 4,我尝试替换soup文本中的“\n\t”字符 这是我的密码: soup = BS(html_doc, "html.parser") for tableItem in soup.find_all("td"): result = str(tableItem.string) result = result.replace("\n\t\", "") print(result) 这是我的作品: \n', '\t\t\t\t\t\t\t

您好,我正在使用BeautifulSoup 4,我尝试替换soup文本中的“\n\t”字符

这是我的密码:

soup = BS(html_doc, "html.parser")
for tableItem in soup.find_all("td"):
    result = str(tableItem.string)
    result = result.replace("\n\t\", "")
    print(result)
这是我的作品:

\n', '\t\t\t\t\t\t\t\t\t\tTEXT_I_WANT\t\t\t\t\t\t\t\t\t
我尝试了一些关于编码或beautifulsoup“Navigablesting”的方法。我是否使用了错误的编码?或者有没有特殊的方法来美化群体。(如剥去的线绳)


ps:我可以替换所需的文本,但不能替换“\n”或“\t”

这行:
result=result.replace(“\n\t\”,”)
查找
\n\t
的所有实例,然后替换它们-它不查找
\n
\t
的单个实例。看来你想要的是:

result = result.replace('\n', '')
result = result.replace('\t', '')
实际上,您需要的不是
字符串
get_text()
还可以删除文本开头和结尾的
\n
\t

soup = BS(html_doc, "html.parser")
for tableItem in soup.find_all("td"):
    print(tableItem.get_text(strip=True))

使用string strip()方法不幸的是strip()对neitherHi没有帮助,谢谢这对我现在起作用了:result=result.replace('\\n','')。我需要两次“\\”,但我也不得不用两个电话分开替换