【问题标题】:Changes not written into a CSV file after setting UTF-8 encoding设置 UTF-8 编码后未写入 CSV 文件的更改
【发布时间】:2021-05-25 11:19:59
【问题描述】:

我正在尝试将更改写入文件,但出现错误:

'charmap' codec can't encode character '\u0159' in position 17: character maps to <undefined>

其他人说你需要将编码设置为UTF-8,所以我设置了:

with open('ScrapedContent.csv', 'w+', newline='', encoding="utf-8") as write

完成此操作后,文本不再写入ScrapedContent.csv 文件,之后整个程序变得几乎毫无用处。这是我的代码:

(我提供了整个代码,因为我不知道问题发生在哪里)

所需的解决方案:

有“特殊”字符写入文件,例如“č、ř、š”。这些在 21 世纪实际上并不特别,而是很正常,但不幸的是,计算机似乎仍然很难理解。

所以无论如何我都需要将这些字符写入文件,以免它们被破坏。只要最终文件提供结果,我不关心必须做什么。我现在花了大约 6 个小时试图解决这个问题,但我一无所获。

这是完整的错误输出:

Traceback (most recent call last):
  File "E:\Projects\Reality Scrapers\SRealityContentScraper\main.py", line 113, in <module>
    writer.writerow([title.text, offers.text, address.text, phone_number, email])
  File "C:\Users\workstationone\AppData\Local\Programs\Python\Python39\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u011b' in position 57: character maps to <undefined>

这是代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import WebDriverException, TimeoutException
from platform import system
from os import getcwd, getlogin
import csv

cwd = getcwd()
os = system()
user = getlogin()
browser = input("Browser name ex.: Chromium | Chrome | Firefox: ")

if os == "Linux":
    if user == "root":
        print(
            "You are executing the script as root. Make sure that the profile folder is also located in the root directory.")
        del user

if browser == "Firefox" or browser == "Firefox ESR" or browser == "Firefox Browser":
    try:
        if os == "Windows":
            driver = webdriver.Firefox(executable_path=cwd + "/geckodriver.exe")
        else:
            driver = webdriver.Firefox(executable_path=cwd + "/geckodriver")
    except WebDriverException:
        print("Warning 10: Firefox is not installed in the default location")
        bin_location = input("Firefox executable location: ")
        binary = FirefoxBinary(bin_location)
        if os == "Windows":
            driver = webdriver.Firefox(executable_path=cwd + "/geckodriver.exe", firefox_binary=bin_location)
        else:
            driver = webdriver.Firefox(executable_path=cwd + "/geckodriver", firefox_binary=bin_location)
        del bin_location

elif browser == "Chrome" or browser == "Chrome Browser" or browser == "Google Chrome" or browser == "Chromium" or browser == "Chromium Browser":
    try:
        if os == "Windows":
            driver = webdriver.Chrome(executable_path=cwd + "/chromedriver.exe")
        else:
            driver = webdriver.Chrome(executable_path=cwd + "/chromedriver")
    except WebDriverException:
        print("Warning 11: Chrome/Chromium is not installed in the default location")
        bin_location = input("Chrome/Chromium executable location: ")
        options = Options()
        options.binary_location = bin_location
        if os == "Windows":
            driver = webdriver.Chrome(executable_path=cwd + "/chromedriver.exe")
        else:
            driver = webdriver.Chrome(executable_path=cwd + "/chromedriver")
        del bin_location

else:
    print("Error 10: Invalid browser selected")
    input("Press ENTER to exit: ")
    exit()

wait = WebDriverWait(driver, 10)

with open('links.csv', 'w+', newline='', encoding="utf-8") as write:
    driver.get("https://www.sreality.cz/adresar")
    writer = csv.writer(write)
    page_spawn = 0
    while page_spawn == 0:
        try:
            links = wait.until(ec.presence_of_all_elements_located((By.CSS_SELECTOR, "h2.title > a")))
            # print(len(links))
            for link in links:
                print(link.get_attribute("href"))
                writer.writerow([link.get_attribute("href")])
            wait.until(ec.element_to_be_clickable(
                (By.CSS_SELECTOR, "a.btn-paging-pn.icof.icon-arr-right.paging-next:not(.disabled"))).click()
        except TimeoutException:
            page_spawn = 1
            break

with open('links.csv') as read:
    reader = csv.reader(read)
    link_list = list(reader)
    with open('ScrapedContent.csv', 'w+', newline='', encoding="utf-8") as write:
        writer = csv.writer(write)
        for link in link_list:
            driver.get(', '.join(link))
            title = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "h1.page-title span.text.ng-binding")))
            offers = wait.until(ec.presence_of_element_located(
                (By.CSS_SELECTOR, "a.switcher.ng-binding.ng-scope span.ng-binding.ng-scope")))
            address = wait.until(
                ec.presence_of_element_located((By.CSS_SELECTOR, "tr.c-aginfo__table__row td.ng-binding")))
            try:
                wait.until(
                    ec.presence_of_element_located((By.CSS_SELECTOR, "button.value.link.ng-binding.ng-scope"))).click()
                phone_number = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "span.phone.ng-binding")))
            except TimeoutException:
                pass
            try:
                wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "button.value.link.ng-binding"))).click()
                email = wait.until(ec.presence_of_element_located((By.CSS_SELECTOR, "a.value.link.ng-binding")))
            except TimeoutException:
                pass
            try:
                phone_number = phone_number.text
            except AttributeError:
                phone_number = " "
                pass
            try:
                email = email.text
            except AttributeError:
                email = " "
                pass
            print(title.text, " ", offers.text, " ", address.text, " ", phone_number, " ", email)
            try:
                writer.writerow([title.text, offers.text, address.text, phone_number, email])
            except Exception as e:
                print (e)
        driver.quit()

【问题讨论】:

  • 我提供了整个代码,因为我不知道问题发生在哪里。如果您提供整个堆栈跟踪,您就会知道,我们也会知道。这可以准确地告诉您代码中的问题所在,这就是它如此重要的原因。
  • 我已经编辑了帖子并提供了完整的错误输出。
  • 如果你只使用 writer.writerow([title.text]) 会不会出错?如果不继续添加字段,直到您找出导致问题的原因。你可能错过了.text 吗?
  • 请不要玷污您的问题,这会给本网站的版主带来不必要的额外工作。一旦您在本网站上发布问题,它就成为本网站的财产,您不应污损或毁坏它。
  • 请 1) 阅读网站的 terms of serviceprivacy policy 以了解您在加入网站之前同意的内容、在欧盟和其他地方合法的条款,以及 2) 停止进行人身攻击直接违反本站发布的Code of Conduct的本站会员。

标签: python python-3.x selenium csv selenium-webdriver


【解决方案1】:

这很大程度上基于this answer

基本上,你不能直接用csv写unicode字符。

你需要一个辅助函数:

 def utf8ify(l):
       return [str(s).encode('utf-8') for s d]

然后当你写行添加:

 writer.writerow(utf8ify([title.text, offers.text, address.text, phone_number, email]))

The answer I linked to 在各方面都比我的好。如果您想了解为什么会这样,请阅读该答案。

【讨论】:

    猜你喜欢
    • 2015-05-18
    • 2014-07-01
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    • 2018-01-16
    • 2015-06-13
    • 1970-01-01
    • 2015-03-04
    相关资源
    最近更新 更多