Very strange behaviour when using string methods in Streamlit

I am getting two different behaviours I don’t understand when using string methods on uploaded .txt files I have decoded to string. When using str.replace() it is adding erroneous new lines, and when using str.splitlines() it is returning just reams of whitespace. I think I perhaps do not fully understand how the file uploader works.

NOTE: I just noticed something when writing this. The erroneous newlines don’t seem to be newlines at all, but strangely large line spacings that VS Code interprets as extra newlines. What is going on?
(Also, I realise my variable name choices need revamping. This is based on quite old code. I will do that as soon as I have the base code working.)

Code snippet for replace():

delimFileOpener = st.file_uploader(label="Select .txt file to add delimeters", type=['txt'], accept_multiple_files=False, help=None, on_change=None, args=None, kwargs=None, disabled=False, label_visibility="visible")
if st.button('Add Delimeters'):
    if delimFileOpener is None:
        st.warning('Please submit a file')
    else:
        delimFile = delimFileOpener.getvalue()
        txtformat.delim(delimFile)

def delim(file): 
       
    
        #read the selected file and apply it to variable elms
    elms = file.decode('utf-8')
        #replace the applicable parts of variable elms with the delimiters
    elms = elms.replace('e+002', 'e+002, ')
    elms = elms.replace('e+003', 'e+003, ')
    elms = elms.replace('e+004', 'e+004, ')
    elms = elms.replace('e+005', 'e+005, ')
    elms = elms.replace('e+006', 'e+006, ')
    elms = elms.replace('e+007', 'e+007, ')
    elms = elms.replace('e+008', 'e+008, ')
    elms = elms.replace('e+009', 'e+009, ')
   

    with open('final_file.txt', 'w') as magma_file:
        #write a file with the stored, altered text in variable elms
        magma_file.write(elms)
    #close the writeable file
    magma_file.close()
    st.success('Delimeters succesfully added')

Code snippet for splitlines() please assume I have uploaded file as done in previous example:

#function to add the prefix elem n where n is the node number
def addElem(file):

    #create counting variables
    counter = 0
    linecount = 1

    #put file as string in variable checks
    checks = file.decode('utf-8')
    checks.splitlines()
    
    
    #check to see if the start of the file is formatted correctly   
    if checks[0].rstrip().endswith('5'):
        with open('final_file.txt', 'w') as ff:            
        #iterate through the lines in the file
            for line in checks:
                counter+=1
                    
                #skip every second (non-node) line
                if counter % 2 == 0:
                    #write the correct prepend with an incrementing number
                    ff.write('Elem ' + str(linecount) + ' ' + checks[line] + '\n')
                    #incrememt the number
                    linecount += 1
                        
                else:
                    #write the (non-node) line without prepend
                    ff.write(line + '\n')
        ff.close()
        st.success('Element labels successfully added.')
    else:
        st.warning("Don\'t forget to format the original file correctly. The first line should read '    1    5'")

Expected behavior:

When doing this locally (not using Streamlit), I usually am able to write delimiters in the first example, and prefix every second line with “Elem n” in the second example (where n is a number incrementing by one)

Actual behavior:
I am getting newlines I didn’t enter between each line with the first example, (see below for visual) and in the second example, when I look inside the variable checks (checks[0], checks [1], checks [2] etc.) all of them are empty.

Debug info

  • Streamlit version: v 1.14.0
  • Python version: 3.9.10
  • Using Conda?
  • OS version: Windows 10
  • Browser version: Mozilla Firefox 106.0.5

Additional information

I am using a .txt file with over 60,000 lines of numbers that look like so:
1 5
2.85446e+007-2.78242e+007-1.69846e+006-1.50888e+007-4.96636e+006 2.08607e+007
2 5
7.26083e+007-1.05617e+007-9.78351e+006-1.40769e+007 8.60043e+006-3.91276e+006

After adding delimiters, it looks like this:
1 5

2.85446e+007, -2.78242e+007, -1.69846e+006, -1.50888e+007, -4.96636e+006, 2.08607e+007,

   2       5

7.26083e+007, -1.05617e+007, -9.78351e+006, -1.40769e+007, 8.60043e+006, -3.91276e+006,

   3       5

-5.64242e+006, -3.21304e+007, -2.14520e+007, -1.99598e+007, -6.45359e+006, -6.65536e+006,