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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:37:06+00:00 2026-05-27T03:37:06+00:00

def process_dialect_translation_rules(): # Read in lines from the text file specified in sys.argv[1], stripping

  • 0
def process_dialect_translation_rules():

    # Read in lines from the text file specified in sys.argv[1], stripping away
    # excess whitespace and discarding comments (lines that start with '##').
    f_lines = [line.strip() for line in open(sys.argv[1], 'r').readlines()]
    f_lines = filter(lambda line: not re.match(r'##', line), f_lines)

    # Remove any occurances of the pattern '\s*<=>\s*'. This leaves us with a 
    # list of lists. Each 2nd level list has two elements: the value to be 
    # translated from and the value to be translated to. Use the sub function
    # from the re module to get rid of those pesky asterisks.
    f_lines = [re.split(r'\s*<=>\s*', line) for line in f_lines]
    f_lines = [re.sub(r'"', '', elem) for elem in line for line in f_lines]

This function should take the lines from a file and perform some operations on the lines, such as removing any lines that begin with ##. Another operation that I wish to perform is to remove the quotation marks around the words in the line. However, when the final line of this script runs, f_lines becomes an empty lines. What happened?

Requested lines of original file:

##  English-Geek Reversible Translation File #1
##   (Moderate Geek)
##  Created by Todd WAreham, October 2009

"TV show"    <=> "STAR TREK"
"food"       <=> "pizza"
"drink"      <=> "Red Bull"
"computer"   <=> "TRS 80"
"girlfriend" <=> "significant other"
  • 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-27T03:37:06+00:00Added an answer on May 27, 2026 at 3:37 am

    In Python, multiple for loops in a list comprehension are handled from left to right, not from right to left, so your last expression should read:

    [re.sub(r'"', '', elem) for line in f_lines for elem in line]
    

    It doesn’t lead to an error as it is, since list comprehensions leak the loop variable, so line is still in scope from the previous expression. If that line then is an empty string you get an empty list as result.

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

Sidebar

Related Questions

I have a text file which I intend to convert to a CSV (in
i found a ruby encryption library which is using this function. def process(text) 0.upto(text.length-1)
I have subclassed Process like so: class EdgeRenderer(Process): def __init__(self,starter,*args,**kwargs): Process.__init__(self,*args,**kwargs) self.starter=starter Then I
When using python-daemon , I'm creating subprocesses likeso: import multiprocessing class Worker(multiprocessing.Process): def __init__(self,
def foo (): x = re.compile('^abc') foo2(x) def foo2(x): How do I get x
def update @album = Album.find(params[:id]) if @album.update_attributes(params[:album]) redirect_to(:action=>'list') else render(:action=>'edit') end end A Rails
def partial(template, *args) options = args.extract_options! options.merge!(:layout => false) if collection = options.delete(:collection) then
def test_method [a, b, c].map {|i| yield(i) } end If I call test_method like
def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj =
def any? if block_given? method_missing(:any?) { |*block_args| yield(*block_args) } else !empty? end end In

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.