Python 刮取和字符串格式问题:使用'';另一次是“与”&引用;

Python 刮取和字符串格式问题:使用'';另一次是“与”&引用;,python,format,beautifulsoup,Python,Format,Beautifulsoup,此代码: url="http://www.royalcanin.fr/nos-aliments/gammes-pour-chiens/tous-les-aliments-pour-chiens/les-aliments-chez-les-veterinaires/chiens-en-bonne-sante/small/chien-sterilise/neutered-adult-small-dog"## read URL from an array coming from an Url-

此代码:

    url="http://www.royalcanin.fr/nos-aliments/gammes-pour-chiens/tous-les-aliments-pour-chiens/les-aliments-chez-les-veterinaires/chiens-en-bonne-sante/small/chien-sterilise/neutered-adult-small-dog"## read URL from an array coming from an Url-CSV
#print(url)

page_0=urllib.request.urlopen(url)
soup_0 = BeautifulSoup(page_0.read(),"html.parser") 
restricted_webpage_title_indication= soup_0.find( "div", {"class":"bloc"} ) # to get title and indication
readable_restricted_title_indication=str(restricted_webpage_title_indication)
soup_title_indication=BeautifulSoup(readable_restricted_title_indication,"html.parser")

indication=[]


for li in soup_title_indication.find_all('li'):
    indication.append(li.get_text().strip())

Pair_indication=["Indications",indication]
print(Pair_indication)
给了我以下打印:

['Indications', ['Risque de prise de poids', 'Sensibilité buccodentaire', "Risque de calculs d'oxalate et de struvite"]]
为什么最后一个元素与前两个元素一样用“”引用,而不是用“”引用? 我不明白的是,在这个网站上,三个“li”的标签和书写方式都是一样的。像这样:

药品价格风险
  • 口腔癌敏感度
  • 草酸和鸟粪石结石风险
  • 为什么会这样?我错过了什么?
    谢谢你的帮助

    您没有遗漏任何内容,Python使用双引号打印了最后一个字符串,因为该字符串的主体已经有了单引号,因此Python没有使用单引号打印并显示内部单引号转义,而是使用双引号打印该字符串

    不管列表的所有元素是字符串,区别仅在打印期间

    这是一个非常简单的例子-

    >>> l = ['123','12\'3']
    >>> l
    ['123', "12'3"]
    >>> repr(l[1])
    '"12\'3"'
    >>> print(repr(l[1]))
    "12'3"
    

    请注意上面是
    repr()
    处理它的方式,当写入csv时,
    csv
    模块将以不同的方式正确处理它。范例-

    >>> l = ['123','12\'3',"222'333"]
    >>> with open('b.csv','w') as f:
    ...     writer = csv.writer(f,quotechar="'")
    ...     writer.writerow(l)
    ...
    24
    >>> with open('b.csv','r') as f:
    ...     reader = csv.reader(f,quotechar="'")
    ...     for line in reader:
    ...             print(line)
    ...
    ['123', "12'3", "222'333"]
    []
    
    csv文件看起来像-

    123,'12''3','222''333'
    

    旁注:虽然有些网站确实应该被废弃,但术语是“废弃”。-)我明白,我感谢你。但是当
    方言.quotechar
    默认设置为
    ''
    时,它确实会导致csv写入问题,不是吗?这是双引号吗?一个单字符字符串,用于引用包含特殊字符的字段,例如分隔符或quotechar,或者包含从
    csv
    文档中获取的新行字符。因此,如果quote char为“”,并且quote字段已经包含“”,那么这里会发生什么事情,或者我错了吗?我不认为这会导致任何问题,请使用我添加的csv模块检查示例。