我正在尝试将列表写入文件。并且csv.writer在带有引号的字符串中写入字符串,即使它们没有引号。我用.strip取出引号,当我通过打印检查它打印就好了。
# -*- coding: utf-8 -*-
'''
##############################################################################
Scrap wiki pages with localities in the US.
'''
import requests
from bs4 import BeautifulSoup
import csv
# list of all links to scrap:
linkList = [
'https://en.wikipedia.org/wiki/List_of_cities_and_towns_in_Alabama',
]
def scrapSite():
scrap = []
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)
scrap.append(loc)
except TypeError:
pass
return scrap
filename = 'localities.csv'
scrap = scrapSite()
def saveFile(scrap, filename):
with open(filename, 'wb') as csvfile:
writer = csv.writer(csvfile, delimiter=',',)
writer.writerow(['Name'])
for loc in scrap:
writer.writerow(loc)
if __name__ == '__main__':
saveFile(scrap, filename)
答案 0 :(得分:1)
要仅在绝对需要时发出引号,请在创建编写器时添加csv.QUOTE_MINIMAL
或csv.QUOTE_NONE
参数:
writer = csv.writer(csvfile, delimiter=',',csv.QUOTE_MINIMAL)
作家之间存在微妙的差异,但基本用法却是相同的:
csv.QUOTE_MINIMAL指示writer对象仅引用包含特殊字符的字段,例如delimiter,quotechar或lineterminator中的任何字符。
csv.QUOTE_NONE:指示编写器对象从不引用字段。当输出数据中出现当前分隔符时,它前面是当前的escapechar字符。如果未设置escapechar,则在遇到任何需要转义的字符时,编写器将引发错误。
当数据必须被引用时,偶数csv.QUOTE_NONE
会发出引号(例如:数据中的引号,如果没有转义,则会导致csv不可读。