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

  • Home
  • SEARCH
  • 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 9175929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:59:51+00:00 2026-06-17T16:59:51+00:00

I’m very new to Python, but I have an aching problem. I have received

  • 0

I’m very new to Python, but I have an aching problem.
I have received a program which reads an infile (text), changes some values, and writes an outfile (also text).
As the outfile grows bigger, the writes get slower and slower, making it unbearably slow after some 2 MB.
Why can this be? I have tried altering the code to use buffers of different sizes, and I have changed it to cache the data into larger chunks (a string) before writing. I also tried join instead of += to create the string to be written. NONE of these do any difference at all to performance – except writing bigger chunks, which actually made the code SLOWER.(!!!)

Here is the method that writes the outfile. I moved the write portion from a separate method to inline:

for ifile in _file_stripper(f_in):
    parse_infile(ifile)
    date = variable_data['arkiveringsdatum']
    variable_data['arkiveringsdatum'] = datetime( int(date[0:4]), int(date[4:6]), int(date[6:8]), tzinfo=TZ()).isoformat('T')
    _personnr= variable_data['personnr'].replace('-', '').split(' ')[0]
    tmplist = ['<utskriftsstatus><brevid_kalla>', variable_data['brevid_kalla'], '</brevid_kalla><mapp>Se Allt</mapp><tidpunkt>', variable_data['arkiveringsdatum'], '</tidpunkt><dokumentpaket>', variable_data['dokumenttyp'], '</dokumentpaket><status>Utskriven</status><rensningsdatum>999999</rensningsdatum><kundid_gdb>', variable_data['kundid_gdb'], '</kundid_gdb><personnr>', _personnr, '</personnr></utskriftsstatus>']
    f_out.write(''.join(tmplist))

Method _file_stripper splits a big file into records.
Infiles are 5-21 MB.

Please advice where to look for the error.
When I talk about slowdown, the write speed falls down below 4KB written/second after around 1 MB has been written, and it keeps falling as the outfile grows bigger.

EDIT: On request, here is parse_infile and _file_stripper:

def parse_infile(f_in):
   index = ""     #variabel som håller vilken ondemandvariabel vi läser in
   found_data = 0  #1 ifall vi hittat det vi letar efter annars 0
   for row in f_in:
      if( 'personnr' in row):
         found_data=1
         index = "personnr"
      elif( 'kundid_gdb' in row):
         found_data=1
         index = "kundid_gdb"
      elif( 'brevid_kalla' in row):
         found_data=1
         index = "brevid_kalla"
      elif( 'arkiveringsdatum' in row):
         found_data=1
         index = "arkiveringsdatum"
      elif( 'GROUP_FILENAME' in row ):
         variable_data['dokumenttyp'] = row.split(':')[-1].split('.')[2].capitalize()
      elif(found_data==1):
         variable_data[index] = row.split(':')[1].strip() 
         index = ""  #Nollställ index ifall värden saknas i filen
         found_data=0
      else:
         pass

def _file_stripper(tot_file):
   try:
      myfile = []
      for rows in tot_file:
         if not 'GROUP_FILENAME' in rows:
            myfile.append(rows)
         else:
            myfile.append(rows)
            yield myfile
   except Exception:
      pass

variable_data = { "brevid_kalla": "", "arkiveringsdatum": "", 
          "kundid_gdb": "", "personnr": "",
          "dokumenttyp": "" }
  • 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-17T16:59:52+00:00Added an answer on June 17, 2026 at 4:59 pm

    Your _file_stripper function adds to the myfile list endlessly, without ever reseting the list:

    def _file_stripper(tot_file):
       try:
          myfile = []
          for rows in tot_file:
             if not 'GROUP_FILENAME' in rows:
                myfile.append(rows)
             else:
                myfile.append(rows)
                yield myfile
       except Exception:
          pass
    

    Note that myfile is set outside the loop, and each row is appened to myfile, then yielded as is. Your process memory footprint will thus grow and grow, forcing the OS to start swapping out memory eventually, thus slowing your process to a crawl.

    I think you meant to reset myfile when GROUP_FILENAME doesn’t appear in rows:

    def _file_stripper(tot_file):
       try:
          myfile = []
          for rows in tot_file:
             if not 'GROUP_FILENAME' in rows:
                myfile.append(rows)
             else:
                myfile.append(rows)
                yield myfile
                myfile = []
       except Exception:
          pass
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a French site that I want to parse, but am running into
I have an autohotkey script which looks up a word in a bilingual dictionary
This could be a duplicate question, but I have no idea what search terms
I have an array which has BIG numbers and small numbers in it. I
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I have a reasonable size flat file database of text documents mostly saved in
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.