Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG+1] Doc: binary mode is required for exporters #2564

Merged
merged 1 commit into from Feb 20, 2017

Conversation

@elacuesta
Copy link
Member

@elacuesta elacuesta commented Feb 14, 2017

Closes #2553

I left XmlItemExporter out of the modifications, it's the only class which does not seem to have problems writing to either bytes or str streams (io.BytesIO or io.StringIO, for instance).

from io import BytesIO, StringIO

with BytesIO(b'') as f:
    exporter = XmlItemExporter(f)
    exporter.start_exporting()
    exporter.export_item({'some': 'value'})
    exporter.finish_exporting()
    print(f.getvalue())

with StringIO(u'') as f:
    exporter = XmlItemExporter(f)
    exporter.start_exporting()
    exporter.export_item({'some': 'value'})
    exporter.finish_exporting()
    print(f.getvalue())

outputs:

b'<?xml version="1.0" encoding="utf-8"?>\n<items><item><some>value</some></item></items>'
<?xml version="1.0" encoding="utf-8"?>
<items><item><some>value</some></item></items>

/cc @kmike

@kmike
Copy link
Member

@kmike kmike commented Feb 14, 2017

While XmlItemExporter works with text files, I'm not sure it is intentional. For example, 'encoding' argument doesn't work properly with XmlItemExporter + text files.

@@ -281,7 +281,8 @@ CsvItemExporter
CSV columns and their order. The :attr:`export_empty_fields` attribute has
no effect on this exporter.

:param file: the file-like object to use for exporting the data.
:param file: the file-like object to use for exporting the data. It's ``write`` method should

This comment has been minimized.

This comment has been minimized.

@elacuesta

elacuesta Feb 14, 2017
Author Member

oops

@elacuesta
Copy link
Member Author

@elacuesta elacuesta commented Feb 14, 2017

Right, the following:

from io import StringIO

with StringIO(u'') as f:
    exporter = XmlItemExporter(f, encoding='utf8')
    exporter.start_exporting()
    exporter.export_item({'some': u'valûe'})
    exporter.finish_exporting()
    print(f.getvalue())

Fails in python 2, it seems like the binary mode is more consistent. I'll add the note to the XmlItemExporter as well. Thanks!

@elacuesta elacuesta force-pushed the elacuesta:docs_exporters branch from 68ff5a1 to 922d3fe Feb 14, 2017
@codecov-io
Copy link

@codecov-io codecov-io commented Feb 14, 2017

Codecov Report

Merging #2564 into master will not change coverage.
The diff coverage is n/a.

@@           Coverage Diff           @@
##           master    #2564   +/-   ##
=======================================
  Coverage   83.59%   83.59%           
=======================================
  Files         161      161           
  Lines        8812     8812           
  Branches     1296     1296           
=======================================
  Hits         7366     7366           
  Misses       1200     1200           
  Partials      246      246

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update afac3fd...922d3fe. Read the comment docs.

@kmike kmike changed the title Doc: binary mode is required for exporters [MRG+1] Doc: binary mode is required for exporters Feb 14, 2017
@kmike
Copy link
Member

@kmike kmike commented Feb 14, 2017

Looks good, thanks @elacuesta!

@elacuesta
Copy link
Member Author

@elacuesta elacuesta commented Feb 14, 2017

Thanks to you and @TDominiqueWilliams 😄

@dangra dangra merged commit 85ef6a6 into scrapy:master Feb 20, 2017
1 check passed
1 check passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@elacuesta elacuesta deleted the elacuesta:docs_exporters branch Feb 20, 2017
@dangra dangra added this to the v1.4 milestone Feb 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants