Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

agdgdgdgwngo

http://ideone.com/Pru2y I need help with a function that cuts off whitespace surrounding newline characters in strings. I've already got it working in Python but I know I can improve it. Funny thing is that on ideone, I have to subtract 1 from the 'newline' variable to get it working right whereas on my Python implementation (2.7.2) it works correctly without that line.

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. rsmith6559
    Best Response
    You've already chosen the best response.
    Medals 0

    Have you looked at/studied regular expressions?

    • 2 years ago
  2. rivermaker
    Best Response
    You've already chosen the best response.
    Medals 1

    If you do not know regex, It may be simpler to use built-in functions: def trimspaces(s): nl = '\n' ss = s.split(nl) ssr = [] for x in ss: ssr.append(x.strip()) return nl.join(ssr) if you know list comprehensions, it is one-liner return '\n'.join([ x.strip() for x in s.split('\n')])

    • 2 years ago
  3. agdgdgdgwngo
    Best Response
    You've already chosen the best response.
    Medals 0

    I knew there was something built-in that would do the trick.... in 1 line! thanks python has a regex library.... but I don't even know what a regex is :(

    • 2 years ago
  4. rsmith6559
    Best Response
    You've already chosen the best response.
    Medals 0

    Regular Expressions are a pattern matching grammar. They're the basis of Perl. Not the easiest things to learn, but extremely powerful. I use them every day to filter log files and do various tasks. Definitely worth the effort of learning.

    • 2 years ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.