Python csv.writer将带引号的字符串写入文件,而不是实际字符串

Python csv.writer将带引号的字符串写入文件,而不是实际字符串,python,Python,我正试图将一个列表写入文件。即使字符串周围没有引号,csv.writer也会使用引号写入字符串。我用了.strip来去掉引号,当我打印出来检查时,它打印的很好 # -*- coding: utf-8 -*- ''' ############################################################################## Scrape wiki pages with localities in the US. ''' import reque

我正试图将一个列表写入文件。即使字符串周围没有引号,csv.writer也会使用引号写入字符串。我用了.strip来去掉引号,当我打印出来检查时,它打印的很好

# -*- coding: utf-8 -*-

'''
##############################################################################
Scrape wiki pages with localities in the US.
'''

import requests
from bs4 import BeautifulSoup
import csv

# list of all links to scrape:

linkList = [
            'https://en.wikipedia.org/wiki/List_of_cities_and_towns_in_Alabama',

]

def scrapeSite():
    scrape = []    
    for link in linkList:
        
        #inside location
        page = requests.get(link)   
        soup = BeautifulSoup(page.text, "lxml")

        table = soup.find_all(scope="row")
        for el in table:
            title = el.find('a')
            try:
                loc = []
                string = title['title'].strip('"')
                loc.append(string)
                scrape.append(loc)
            
            except TypeError:
                pass
          
    return scrape



filename = 'localities.csv'

scrape = scrapeSite()

def saveFile(scrape, filename):

    with open(filename, 'wb') as csvfile:
            
            writer = csv.writer(csvfile, delimiter=',',)
       
            writer.writerow(['Name']) 
            for loc in scrape:  
                writer.writerow(loc)


if __name__ == '__main__':
    saveFile(scrape, filename)

要仅在绝对需要时发出引号,请在创建编写器时添加csv.QUOTE_MINIMAL或csv.QUOTE_NONE参数:

writer = csv.writer(csvfile, delimiter=',',csv.QUOTE_MINIMAL)
作家之间有细微的差别,但基本用法是一样的:

csv.QUOTE_MINIMAL指示writer对象仅引用包含特殊字符(如分隔符、引号或lineterminator中的任何字符)的字段

csv.QUOTE_NONE:指示writer对象从不引用字段。当当前分隔符出现在输出数据中时,它前面会有当前转义字符。如果未设置escapechar,则如果遇到任何需要转义的字符,写入程序将引发错误

即使csv.QUOTE_NONE在数据必须被引用时也会发出引号,例如:数据中的引号,如果不转义,这将使csv无法读取。

try writer=csv.writerscsvfile,delimiter=',',csv.QUOTE_