It is not clear if, as you say in the question, you add a line break when reading the file, or if, as other users have answered, the line break occurs > when printing what you have read.
Since nobody has yet answered how to solve it if it were the first case ( to read ), I add this answer. The screenshot that you include does not help much either, since you can not clearly see where that extra car return you mentioned would be.
The method .rstrip()
of the chains is used to eliminate spaces or carriage returns that the chain may contain at the end. You also have .lstrip()
to delete the ones you can contain at the beginning and .strip()
for both sides at the same time. Therefore:
with open('uncarb.txt') as infile:
x=infile.read().strip()
print(x)
And by the way, although you did not ask this, it seems that what you read from the file is a string that contains the ASCII representation of a list (perhaps JSON). Since you say that then you are going to process that data, you will need to convert it to a list of python numbers. In this case you can do:
lista = eval(x)
Or, if you prefer not to take security risks (you do not trust that the content of the file you are reading may contain dangerous python code that would be executed as part of eval()
), you can remove the brackets that delimit the list and then Separate it by commas, like this:
x = x.replace("[", "").replace("]", "")
lista = [ int(n) for n in x.split(",") ]