Joseph Langley
Joseph Langley

Reputation: 115

Python 3 - WRITE text to file without \n

I am writing a script that analyses some experimental data, then outputs certain information to a text file. The text file is used by another application, which will not understand the \n markers, and in which I do not have the ability to remove \n markers. How can I write to a text file without \n markers?

Relatively new to Python.

I understand that reading the file in Python would become a problem without \n (is it always? Can't you read text files created by other applications?) But I have thought of a way around this if necessary.

##Create MAPDL material data text file
file = open('MAPDL_material.txt', 'w')
file.write('TB, MELAS')
file.close()

def MAPDL_add_line(j,MAPDL_strain,MAPDL_stress): # Definition to add data to text file
    # Add text to first line (requires to open, read, and completely rewrite)
    file = open('MAPDL_material.txt', 'r')
    file_text = file.readlines()
    file.close()

    file_text[0] = file_text[0] + ',' + str(j)

    file = open('MAPDL_material.txt', 'w')
    for i in file_text:
        file.write(i + "/n")
    file.close()

    # Append line to end of file for new datapoint
    file = open('MAPDL_material.txt', 'a')
    file.write('TBPT, ,'+str(MAPDL_strain)+','+str(MAPDL_stress))
    file.close()
    print('Row '+str(j)+' added to MAPDL material file.')

The problem is that the text file is like this:

TB, MELAS,1/nTBPT, ,0.005,33

But needs to be like this, for the other application to read it:

TB, MELAS,1
TBPT, ,0.005,33

Upvotes: 2

Views: 1093

Answers (1)

Joseph Langley
Joseph Langley

Reputation: 115

Was a rookie error.

/n

should have been:

\n

Upvotes: 1

Related Questions