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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:30:23+00:00 2026-06-18T16:30:23+00:00

So I’m trying to get better at python in general but I’m having some

  • 0

So I’m trying to get better at python in general but I’m having some trouble using the re module for regular expressions.

I have a comma separated csv file that I’m reading in, and then I want to find all occurrences of a line ending in a comma 5. So I used the code below:

    five_rating = re.compile(r",5$", re.MULTILINE)
    print five_rating.findall(file.read())

but I don’t get any output. There are definitely occurrences that match the regular expression I’m using, I’ve tested my regex on python regex websites and they model what I want, but in code, it just doesn’t work!

Is there something obvious I’m doing wrong here?

Oh and I’m using Ubuntu and the file should have DOS style line endings, but I tried converting the end-line characters using the code from this post and it didn’t do the trick.

btw here’s a sample of the input:

9605,Ace Ventura: Pet Detective,5
9606,Ace Ventura: Pet Detective,1
9607,Ace Ventura: Pet Detective,4
9608,Ace Ventura: Pet Detective,3
9609,Ace Ventura: Pet Detective,2
9610,Ace Ventura: Pet Detective,4
9611,Ace Ventura: Pet Detective,3
9612,Ace Ventura: Pet Detective,4
9613,Ace Ventura: Pet Detective,5
9614,Ace Ventura: Pet Detective,5
9615,Ace Ventura: Pet Detective,4
9616,Ace Ventura: Pet Detective,1
9617,Ace Ventura: Pet Detective,3
9618,Ace Ventura: Pet Detective,4
9619,Ace Ventura: Pet Detective,3
9620,Ace Ventura: Pet Detective,1
9621,Ace Ventura: Pet Detective,2
9622,Ace Ventura: Pet Detective,3
9623,Ace Ventura: Pet Detective,5
9624,Ace Ventura: Pet Detective,2
9625,Ace Ventura: Pet Detective,2
9626,Ace Ventura: Pet Detective,4
9627,Ace Ventura: Pet Detective,3
9628,Ace Ventura: Pet Detective,1
  • 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-18T16:30:24+00:00Added an answer on June 18, 2026 at 4:30 pm

    Given you input (which could be a file) as a multiline string, like this:

    st='''9605,Ace Ventura: Pet Detective,5
    9606,Ace Ventura: Pet Detective,1
    9607,Ace Ventura: Pet Detective,4
    9608,Ace Ventura: Pet Detective,3
    9609,Ace Ventura: Pet Detective,2
    9610,Ace Ventura: Pet Detective,4
    9611,Ace Ventura: Pet Detective,3
    9612,Ace Ventura: Pet Detective,4
    9613,Ace Ventura: Pet Detective,5
    9614,Ace Ventura: Pet Detective,5
    9615,Ace Ventura: Pet Detective,4
    9616,Ace Ventura: Pet Detective,1
    9617,Ace Ventura: Pet Detective,3
    9618,Ace Ventura: Pet Detective,4
    9619,Ace Ventura: Pet Detective,3
    9620,Ace Ventura: Pet Detective,1
    9621,Ace Ventura: Pet Detective,2
    9622,Ace Ventura: Pet Detective,3
    9623,Ace Ventura: Pet Detective,5
    9624,Ace Ventura: Pet Detective,2
    9625,Ace Ventura: Pet Detective,2
    9626,Ace Ventura: Pet Detective,4
    9627,Ace Ventura: Pet Detective,3
    9628,Ace Ventura: Pet Detective,1'''
    

    This works:

    import re
    
    for line in st.splitlines():
        m=re.search(r'(^.*,5$)',line)
        if m: print m.group(0) 
    

    or a re.findall version:

    print re.findall(r'(^.*,5$)',st, re.MULTILINE)
    

    or (somewhat confusingly IMHO) re.findall will work without parens:

    print re.findall(r'^.*,5$',st, re.MULTILINE)
    

    Yours is not working because of no .* meaning ‘match everything up to the ‘,5$’

    Also as stated in one of the comments, using file as a identifier is a bad idea.

    You can also use Python’s string processing to do this:

    for line in st.splitlines():
        if line.endswith(',5'): print line
    

    And if you really have a CSV file to process — use the builtin CSV module.


    Finally — if you have a DOS file on *nix, just use Python’s universal line support by using open with ‘U’ in it:

    with open(...,'rU') as infile:
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build
This could be a duplicate question, but I have no idea what search terms
I have thousands of HTML files to process using Groovy/Java and I need to
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.