【问题标题】:Scrapy - encoding csv file utf-8Scrapy - 编码 csv 文件 utf-8
【发布时间】:2018-01-16 18:54:35
【问题描述】:

我浏览了这个网站,我在谷歌上做了同样的事情,但我没有发现关于将数据导出到编码 utf-8 的 csv 文件中。

我需要对我的文件进行编码,因为我有一些法语字符(例如 É)。 我使用 CsvItemExporter,它通常已经以 utf-8 编码,但它没有给我正确的字符。而不是这些字符,我只有一些奇怪的东西,比如 \A4ybzkzv,我不知道如何拥有正确的东西。

我希望我已经足够清楚了!谢谢你的帮助...

这是我的 pipelines.py :

# -*- coding: utf-8 -*-
from scrapy import signals
from scrapy.exporters import CsvItemExporter

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

# Define your output file.
class FnacPipeline(CsvItemExporter):
    def __init__(self):
        self.files = {}

    @classmethod
    def from_crawler(cls, crawler):
        pipeline = cls()
        crawler.signals.connect(pipeline.spider_opened, signals.spider_opened)
        crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
        return pipeline

    def spider_opened(self, spider):
        f = open('..\\..\\..\\..\\Fnac.csv', 'w').close()
        file = open('..\\..\\..\\..\\Fnac.csv', 'w')
        self.files[spider] = file
        self.exporter = CsvItemExporter(file)
        self.exporter.start_exporting()

    def spider_closed(self, spider):
        self.exporter.finish_exporting()
        file = self.files.pop(spider)
        file.close()

    def process_item(self, item, spider):
        self.exporter.export_item(item)
        return item

使用此管道,我有一个错误,而不是正确的字符:TypeError: must be str, not bytes,当我将file = open('..\\..\\..\\..\\Fnac.csv', 'w') 更改为file = open('..\\..\\..\\..\\Fnac.csv', 'wb') 时,我没有更多错误但不是正确的字符...

我的输出: France métropolitaine

我想要的输出: France métropolitaine

【问题讨论】:

  • 快速提问,你为什么不使用内置的scrapy 导出器?您可以使用-o 标志简单地导出结果:scrapy crawl myspider -o results.csv 或者您也可以设置some settings to do that
  • @Granitosaurus 这么说,我认为一些设置或自定义正是他正在做的事情。 :)
  • 由于我有很多项目,有时在同一个项目中有很多蜘蛛,我想通过蜘蛛创建一个 csv 文件......我通过一个可执行文件自动运行我的蜘蛛。所以我不想每次我想运行蜘蛛时都重写我的 exe 文件:)
  • 我不确定我是否理解您的评论,因为我已经使用了内置的 scrapy 导出器,不是吗?我是scrapy的新手,如果我错了,请原谅我......
  • 你可以在每个蜘蛛中使用自定义设置,蜘蛛类变量custom_settings,见docs。除此之外,-o 应该处理所有编码问题,因为默认情况下它应该输出 utf8。

标签: python csv encoding utf-8 scrapy


【解决方案1】:

在 Python 3 中使用 utf-8 编码打开 text 文件的正确方法如下:

fd = open(path, mode='w', encoding='utf-8')
fd.write("Unicode string")

但是您的CsvItemExporter 接缝为您进行编码,因此它将二进制数据写入您的文件。所以最好的方法是以二进制格式打开你的文件:

fd = open(path, mode='wb')
fd.write(b"Binary string")

结果:“France métropolitaine”是正确的。问题是您没有使用正确的编辑器来阅读您的文件。您肯定在使用 Excel。 Excel 在法语版本上默认使用 cp1252 打开 CSV 文件。您需要导入文件才能选择源编码。注意:使用 Libre Office 不会有这个问题。

【讨论】:

  • 我刚刚按照您的说法打开了我的文件(在 excel 上,导入数据...),您是对的...我没有以正确的方式打开它...谢谢! !
【解决方案2】:

所以正确的答案是将其保存为utf-8 并使用excel Import 来查看该属性。

另一方面,您可以通过直接打开它在 Excel 中查看它,但默认编码是cp12523

对我来说,我不能只告诉我的客户使用 excel 的 Import,所以我选择将编码更改为 cp1252,因此无法正确查看。

当您更改settings.py 中的配置时,设置为FEED_EXPORT_ENCODING = 'utf-8' 将不起作用。

我所做的是更改spider_opened函数下的pipelines.py

self.exporter = CsvItemExporter(file, encoding='cp1252')

【讨论】:

    【解决方案3】:
    def open_spider(self, spider):
             self.csvfile  = open(self.filename, 'wb')
             self.exporter = CsvItemExporter(self.csvfile, encoding='utf-8-sig')
             self.exporter.start_exporting()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-04
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 2015-06-13
      相关资源
      最近更新 更多