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 8037381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:54:51+00:00 2026-06-05T02:54:51+00:00

I have a few csv files that contain strings with line breaks in them.

  • 0

I have a few csv files that contain strings with line breaks in them. The files will open just fine in Excel, but when I try to read in the files with csv.DictReader(), csv.DictReader() appears to handle each line break inside the strings as a new row of data rather than ignoring the carriage breaks within strings.

What can I do to get the second test below to pass just as the first test does?

#csv contents
this, is, a, test
1,2,u'thr\nee',4
5,6,7,8 

        result = []
        text = """this, is, a, test
1,2,u'three',4
5,6,7,8"""
        b = StringIO(text)
        reader = csv.DictReader(b)
        for row in reader:
                result.append(row)

        self.assertEqual(2,len(result))
        expected = [{'this': '1', ' test': '4', ' is': '2', ' a': "u'three'"}, {'this': '5', ' test': '8', ' is': '6', ' a': '7'}]
        self.assertEqual(expected ,result)

        #With a /n inside the string.

        result = []
        text = """this, is, a, test
1,2,u'thr\nee',4
5,6,7,8"""
        b = StringIO(text)
        reader = csv.DictReader(b)
        for row in reader:
                result.append(row)

        self.assertEqual(2,len(result))
        #expected = [{'this': '1', ' test': '4', ' is': '2', ' a': "u'thr\nee'"}, {'this': '5', ' test': '8', ' is': '6', ' a': '7'}]
        #self.assertEqual(expected,result)
  • 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-06-05T02:54:53+00:00Added an answer on June 5, 2026 at 2:54 am

    Assuming your csv content is properly quoted, specifying the appropriate quotechar when instanciating the reader should do:

    http://docs.python.org/release/2.6.7/library/csv.html#csv.Dialect.quotechar

    Else, I assume you have unix newlines (‘\n’) in your content and dos newlines (‘\r\n’) as line terminators. As of Python 2.6.7, the doc mentions that the reader is hardcoded to recognise both as line termitors whatever you specify, don’t know it if it’s the case with your Python version. If yes, you’ll have to manually preprocess (and possibly postprocess) your files to either ensure appropriate quoting or replace single ‘\n’ with something else then do the reverse after csv parsing.

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

Sidebar

Related Questions

I have a Python script that we're using to parse CSV files with user-entered
I'm using XLWT to write excel files from .csv and I have the first
If I have few csv files as follows: a,1,2,3 type, max, min, avg b,4,5,6
I have a large collection of data in an excel file (and csv files).
I'm building an editor that works with .CSV files. I have the application importing
I have a few queries regarding columns in Postgres CSV Log. Query 1 Following
I have few PC's that run on slow Internet connection network + they use
I have few textblocks, with custom font family which work fine: FontFamily={StaticResource CodeBold} This
I have few classes that I need to annotate with a name so I
I have the following data that basically I only need a few bits of

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.