Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7435267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:02:44+00:00 2026-05-29T10:02:44+00:00

Consider the following (Python 3.2 under Windows): >>> import io >>> import csv >>>

  • 0

Consider the following (Python 3.2 under Windows):

>>> import io
>>> import csv
>>> output = io.StringIO()         # default parameter newline=None
>>> csvdata = [1, 'a', 'Whoa!\nNewlines!']
>>> writer = csv.writer(output, quoting=csv.QUOTE_NONNUMERIC)
>>> writer.writerow(csvdata)
25
>>> output.getvalue()
'1,"a","Whoa!\nNewlines!"\r\n'

Why is there a single \n – shouldn’t it have been converted to \r\n since universal newlines mode is enabled?

With this enabled, on input, the lines endings \n, \r, or \r\n
are translated to \n before being returned to the caller.
Conversely, on output, \n is translated to the system default line
separator, os.linesep.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:02:45+00:00Added an answer on May 29, 2026 at 10:02 am

    The “single” \n occurs as a data character inside the third field. Consequently that field is quoted so that a csv reader will treat it as part of the data. It is NOT a “line terminator” (should be called a row separator) or part thereof. To get a better appreciation of the quoting, remove the quoting=csv.QUOTE_NONNUMERIC.

    The \r\n is produced because csv terminates rows with the dialect.lineterminator whose default is \r\n. In other words, the “universal newlines” setting is ignored.

    Update

    The 2.7 and 3.2 docs for io.StringIO are virtually identical as far as the newline arg is concerned.

    The newline argument works like that of TextIOWrapper. The default is
    to do no newline translation.

    We’ll examine the first sentence below. The second sentence is true for output, depending on your interpretation of “default” and “newline translation”.

    TextIOWrapper docs:

    newline can be None, ”, ‘\n’, ‘\r’, or ‘\r\n’. It controls the
    handling of line endings. If it is None, universal newlines is
    enabled. With this enabled, on input, the lines endings ‘\n’, ‘\r’, or
    ‘\r\n’ are translated to ‘\n’ before being returned to the caller.
    Conversely, on output, ‘\n’ is translated to the system default line
    separator, os.linesep. If newline is any other of its legal values,
    that newline becomes the newline when the file is read and it is
    returned untranslated. On output, ‘\n’ is converted to the newline.

    Python 3.2 on Windows:

    >>> from io import StringIO as S
    >>> import os
    >>> print(repr(os.linesep))
    '\r\n'
    >>> ss = [S()] + [S(newline=nl) for nl in (None, '', '\n', '\r', '\r\n')]
    >>> for x, s in enumerate(ss):
    ...     m = s.write('foo\nbar\rzot\r\n')
    ...     v = s.getvalue()
    ...     print(x, m, len(v), repr(v))
    ...
    0 13 13 'foo\nbar\rzot\r\n'
    1 13 12 'foo\nbar\nzot\n'
    2 13 13 'foo\nbar\rzot\r\n'
    3 13 13 'foo\nbar\rzot\r\n'
    4 13 13 'foo\rbar\rzot\r\r'
    5 13 15 'foo\r\nbar\rzot\r\r\n'
    >>>
    

    Line 0 shows that the “default” that you get with no newline arg involves no translation of \n (or any other character). It is certainly NOT converting '\n' to os.linesep

    Line 1 shows that what you get with newline=None (should be the same as line 0, shouldn’t it??) is in effect INPUT universal newlines translation — bizarre!

    Line 2: newline='' does no change, like line 0. It is certainly NOT converting '\n' to ''.

    Lines 3, 4, and 5: as the docs say, '\n' is converted to the value of the newline arg.

    The equivalent Python 2.X code produces equivalent results with Python 2.7.2.

    Update 2 For consistency with built-in open(), the default should be os.linesep, as documented. To get the no-translation-on-output behaviour, use newline=''. Note: the open() docs are much clearer. I’ll submit a bug report tomorrow.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider the following python code: import gtk class MainWindow(): def __init__(self): self.window = gtk.Window()
Consider the following Python code: import os print os.getcwd() I use os.getcwd() to get
Consider the following Python code: >>> re.search(r'.*(99)', 'aa99bb').groups() ('99',) >>> re.search(r'.*(99)?', 'aa99bb').groups() (None,) I
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Consider the following function:
Consider the following Python code: In [1]: import numpy as np In [2]: import
This is with Python 2.6.6 (default) on Debian Squeeze. Consider the following Python code.
Consider the following snippet of Python code: from sqlalchemy import * from sqlalchemy.orm import
Consider the following setup: A windows PC with a LAN interface and a WiFi
Consider the following birthdays (as dob ): 1-Jun-68 1-Jun-69 When parsed with Python’s datetime.strptime(dob,
Consider the following Python exception: [...] f.extractall() File C:\Python26\lib\zipfile.py, line 935, in extractall self.extract(zipinfo,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.