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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:57:12+00:00 2026-05-25T15:57:12+00:00

In python, is there a built-in way to do a readline() on string? I

  • 0

In python, is there a built-in way to do a readline() on string? I have a large chunk of data and want to strip off just the first couple lines w/o doing split() on the whole string.

Hypothetical example:

def handleMessage(msg):
   headerTo  = msg.readline()
   headerFrom= msg.readline()
   sendMessage(headerTo,headerFrom,msg)

msg = "Bob Smith\nJane Doe\nJane,\nPlease order more widgets\nThanks,\nBob\n"
handleMessage(msg)

I want this to result in:
sendMessage("Bob Smith", "Jane Doe", "Jane,\nPlease order...")

I know it would be fairly easy to write a class that does this, but I’m looking for something built-in if possible.

EDIT: Python v2.7

  • 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-25T15:57:13+00:00Added an answer on May 25, 2026 at 3:57 pm

    In Python 3, you can use io.StringIO:

    >>> msg = "Bob Smith\nJane Doe\nJane,\nPlease order more widgets\nThanks,\nBob\n"
    >>> msg
    'Bob Smith\nJane Doe\nJane,\nPlease order more widgets\nThanks,\nBob\n'
    >>>
    >>> import io
    >>> buf = io.StringIO(msg)
    >>> buf.readline()
    'Bob Smith\n'
    >>> buf.readline()
    'Jane Doe\n'
    >>> len(buf.read())
    44
    

    In Python 2, you can use StringIO (or cStringIO if performance is important):

    >>> import StringIO
    >>> buf = StringIO.StringIO(msg)
    >>> buf.readline()
    'Bob Smith\n'
    >>> buf.readline()
    'Jane Doe\n'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering if there's a reason that there's no first(iterable) in the Python built-in
Is there a way to run the BASH built-in commands from Python? I tried:
Is there a built-in method in Python to get an array of all a
Is there a built-in method in Python to execute a system command without displaying
There is no built in reverse method for Python's str object. How can I
There are so many html and xml libraries built into python , that it's
In Python is there any way to make a class, then make a second
Is there a good, succinct/built-in way to see if all the values in an
I'm wondering if there is a built-in way to do this... Take this simple
Is there any way to install Setuptools for Python 2.6 in Windows without having

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.