getting rid of the '\n' when writing to a .txt file

i recently started to experimenting with python. after scripting my mathematics homework, i’m now able to write data to a file.

after pasting ’
'behind the string, it makes a newline, but the
is also visible. is there a way to make a newline and a text without
at the end of all lines?

text in my file at the moment:

('hello there', '
')
hi')

also everything i type , is gone the next time i run my script and add something new.

>>> print('hello wereld
hello wereld
hello wereld')
hello wereld
hello wereld
hello wereld

= \ is the escape character, n forces a new line
print('hello there
')

i recommend

we can give you all the answers, but that won’t teach you anything :slight_smile:


dreg = open("poost.txt", 'w')

for i in range(20):
    dreg.write("yoooo hahah gaaar
")

dreg.close()

it seems that you are typing your code into what is called the ‘interpreter’ it starts lines with ‘>>>’ , this refreshes/flushes after you close the interpreter.

are you using blender as your coding tool? if-so you can create a new text file in the Text Editor, and save it as a somename.py , which you can later open to continue working on at a later date. But really, do some python tutorials :slight_smile:

i know that
starts a new line. the problem so far is that the text in my python file is text, but in the .txt file it adds the brackets and ', where i want just simple text, like hello world, now there would stand (‘hello world’)

so:
file.write(‘hello world’) prints hello world in IDLE, but (‘hello world’) in notepad. is there a way to just write text, and not code tot a .txt file?

Show us the code you are using to print to the file.

filepath = 'c:/ffp/a.txt'
with open(filepath,'r+') as file:
    file.write('
')
    i = input('what should be in the file? ')
    i = (i,'
')
    i = str(i)
    n = '
'
    i = i+n
    i = str(i)
    file.write(i,)
    read1 = file.read()    
    print(read1)
 

that’s the code
the line file.write’
'does nothing so far.


that’s the code : the line file.write(’
')
does nothing so far.

firstly, the string you printing with print(read1) does contain the ’
’ so it should display that. You shouldn’t however see
that ‘silent’ formatting character if you use notepad to open that .txt

you use r+ , but i’m not sure you’ve read the documentation to know what that does : http://docs.python.org/py3k/library/functions.html?highlight=open#open

i’m going to rewrite your code slightly.

filepath = 'c:/ffp/a.txt'
with open(filepath,'r+') as file:
    file.write('
')
    i = input('what should be in the file? ')
    i = (i,'
')
    i = str(i)
    n = '
'
    i = i+n
    i = str(i)
    file.write(i,)
    read1 = file.read()    
    print(read1)
 

becomes


filepath = 'c:/ffp/a.txt'
with open(filepath,'r+') as file:
    # file.write('
') #  this is not part of your problem
    i = input('what should be in the file? ')
    i = str(i) # was your input was an integer?
    i = i +'
'

    # the comma here should cause writing to continue on the same line, 
    # but if you are using '
' you end up with new lines anyway
    file.write(i,)  

    read1 = file.read()

    # this displays the exact string, 
    # including the invisible (from notepad) '
' formatting character.
    print(read1) 
                      



Also in the original code:

i = (i, ’
') creates a Tuple. To properly convert it to a string with the elements concatenated use: i = “”.join(i)

thanks so far, going to try that code asap.
and i did read the documentation, r+ means you can both read and write to the file

update:
the new code from zeffii works for the
and brackets, they dissapeared. leaves me with the problem that
when i run the script, and type 1, then run the script again, and type 2, only the 2 is in the file, and the 1 has been deleted

you need to tell it not to start at the beginning, once you reopen the file.
http://docs.python.org/tutorial/inputoutput.html#methods-of-file-objects 7.2.1. Methods of File Objects dig into .seek() and .read()

getting this error
io.UnsupportedOperation: can’t do nonzero end-relative seeks

when i want it to be 1 byte before the end… with: file.seek(-1, 2) - 0 doen’t seem to work.
in the documentation theres -3, 2, saying going three bytes before the end of the file. i want to be at the end so -0 seems logical to me.

tips?

nope, there is no sign to 0 (unlike infinity which has a + or - sign). We start counting from 0 and after many hours of programming you will gradually appreciate this convenience.

consider a different but similar example


mystring = "abcdefg"
mystring[-1] = g

look at 6.2.1 Working with File Objects: http://diveintopython.org/file_handling/file_objects.html

experiment some more, see what .tell() does and see if seek(0,2) isn’t exactly what you are looking for. You can read the last character of the file to check if its a ’
’ (it’s considered one ‘character’)

psuedo code here :


open file
storevalue = seek(last byte)
if storevalue is not '
':
    then if we want to write we first inject a '
' if we want a new line.

well i made a different approach. i thougt, i can also go to the beginning, create a new line, and type something there. odd thing is that when i prent like ‘1234567890’ , run the script again, and print hey, the output(data in the .txt file) is hey (enter) 67890

so the new data is still deleting old data, like insert is turned on…

ah right, my mistake :


with open(filepath,'r+') as file:
    # force end 
    file.seek(0,2) 
    i = input('what should be in the file? ')
    i = str(i) # was your input an integer?
    i = i +'
'

    # the comma here should cause writing to continue on the same line, 
    # but if you are using '
' you end up with new lines anyway
    file.write(i,)  

it works, thanks alot. now i’ll have a look at the code, to understand it even more :slight_smile: