Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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替换文本美化组_Python_Beautifulsoup - Fatal编程技术网

Python替换文本美化组

Python替换文本美化组,python,beautifulsoup,Python,Beautifulsoup,我对这一点python有一个问题。它可以工作,但如果任何数据包含逗号,它显然会弄乱我的CSV import requests import sys from bs4 import BeautifulSoup url = requests.get(sys.argv[1]) html = BeautifulSoup(url.content,'html.parser') for br in html.find_all("br"): br.replace_with(" ") for tr

我对这一点python有一个问题。它可以工作,但如果任何数据包含逗号,它显然会弄乱我的CSV

import requests
import sys
from bs4 import BeautifulSoup

url = requests.get(sys.argv[1])

html = BeautifulSoup(url.content,'html.parser')

for br in html.find_all("br"):
    br.replace_with(" ")

for tr in html.find_all('tr'):
    data = []   

    for td in tr.find_all('td'):
        data.append(td.text.strip())

    if data:
        print("{}".format(','.join(data)))
可以用空格代替逗号吗

例如,来自

column 1    column 2    column,3    column 4

现在我明白了,这是造成问题的原因

column 1,column 2,column,3,column 4

尝试
data.append(td.text.strip().replace(',','')

在添加到数据之前是否尝试用空格替换逗号?
data.append(td.text.strip().replace(',','')
它按预期工作。您的输入是
第1列第2列、第3列第4列,因此它将相应地返回
第1列、第2列、第3列、第4列
,因为您有
,默认情况下,
在第3列之后导入re
,然后只需执行
您的格式化数据=re.findall(r“[\w]+\s[\d]”)
对于格式化数据中的数据:output.append(data+“,”)
谢谢@KeyurPotdar这正是我需要的。
column 1,column 2,column,3,column 4