1336

I have a text file that looks like:

ABC
DEF

How can I read the file into a single-line string without newlines, in this case creating a string 'ABCDEF'?


For reading the file into a list of lines, but removing the trailing newline character from each line, see How to read a file without newlines?.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
klijo
  • 15,761
  • 8
  • 34
  • 49
  • 2
    Related: [How to read a file line by line into a list with Python](http://stackoverflow.com/q/3277503/3345375) – jkdev Oct 31 '16 at 22:22
  • 13
    The title and the question are inconsistent. Do you really want to get rid of the \n as well? – Julian Jul 18 '17 at 10:11
  • 4
    do you really want to remove newlines from the file/string contents, or are you just confused about the many meta-characters in your print output and actually want to keep the newlines, but not have them display as "\n"? – mnagel Dec 05 '17 at 18:25
  • Do you really want to read the entire text into **one string variable**? Do you really mean with "strip newlines" to replace them with an empty string? This would mean, that the last word of a line and the first word of the next line are joined and not separated. I don't know your use case, but this seems to be a strange requirement. I might have another answer if you explain what you intend to do with the read in data – gelonida Jan 07 '21 at 00:47
  • @gelonida based on the answers that were given, the original question text, and which answer was accepted, that is exactly what OP wanted. The first version of the question read: "As i see data is in list form. How do i make it string. And also how do i remove \n, [, and ] characters from it ?" - suggesting severe misunderstanding of the fundamentals, but pretty adamant and clear about the desired output. – Karl Knechtel Aug 09 '22 at 02:21
  • This has, for better or worse, become the closest thing we have to a canonical for the question of how to read an entire file into a single string. – Karl Knechtel Aug 09 '22 at 02:24

27 Answers27

1871

You could use:

with open('data.txt', 'r') as file:
    data = file.read().replace('\n', '')

Or if the file content is guaranteed to be one line:

with open('data.txt', 'r') as file:
    data = file.read().rstrip()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sleeplessnerd
  • 21,853
  • 1
  • 25
  • 29
  • 97
    Is there a downside in just writing `open("data.txt").read().replace('\n','')` instead? – tuomassalo Oct 18 '13 at 10:43
  • 365
    Yes, your version does not explicitly close the file, that will then be delayed until the garbage collector runs or the program terminates. The 'with' statement usually encapsulates some setup/teardown open/close actions. – sleeplessnerd Oct 18 '13 at 13:38
  • 14
    Thanks for the clarification. So, it seems that my version might be ok for small scripts - but OTOH it should preferably be avoided altogether to not make it a habit. – tuomassalo Oct 20 '13 at 17:18
  • 14
    @tuomassalo it is a huge PITA in the test/debug process, as it won't clean up the open file handles if you have to terminate prematurely or it runs into an exception. – GoingTharn Oct 24 '13 at 20:41
  • 1
    that `replace('\n','')` could be simply `rstrip('\n')` – Louis Maddox Jul 05 '14 at 20:27
  • 17
    No, `rstrip('\n')` will only remove the newline from the last line, `replace('\n','')` removes it everywhere (essentially making the whole file one line) – sleeplessnerd Jul 06 '14 at 08:00
  • 3
    You don't need to state the `"r"` explicitely, as it is the default value for the `mode` option (but it might be good just for clarification). – mozzbozz Nov 07 '14 at 10:07
  • 3
    -1 because the `replace('\n')` will change the formatting of the input data. xiaoyu's answer is more accurate. (The `\n` and `[]` appear only because of the use of `readline()`.) – Skippy le Grand Gourou Sep 11 '15 at 09:41
  • @SkippyleGrandGourou: You are mistaken, the `\n` (newline character) is contained in the input. `readlines()` splits at occurrences of it, but leaves them intact. – sleeplessnerd Nov 11 '15 at 18:37
  • 2
    @sleeplessnerd : Indeed my comment parenthesis is poorly worded, but that's precisely my point : the OP doesn't *really* want to get rid of them. See also Alex Dupuy's comment to xiaoyu's answer. – Skippy le Grand Gourou Nov 15 '15 at 21:00
  • 1
    this question/answer ranks very high on search engines for people looking to read a file into a string. unfortunately this snippet does something else/destructs newlines. probably not out of spite, but simply because the original author of the question is bit confused about the meta-characters in his output... BUT THIS IS PROBABLY NOT what most people looking for `def as_string(filepath)` are looking for! – mnagel Dec 05 '17 at 18:23
  • Can I access data when file is closed? If I don't indent `data` under `with`? – Hrvoje T Apr 25 '18 at 12:46
  • Is it really the case that we have to wait for the GC to close the file if we do not explicitly close it? I thought Python also has reference counting (and GC just for cyclic refs). After the line data = open(...).read() there will be no reference to the file object anymore and it can be closed. – Nils Dec 11 '20 at 13:37
  • @Nils It depends entirely on the implementation of python when a file is closed, that does not use the `with` statement. Up to my knowledge C-python would destroy the file handle as soon as the file object 's reference counter goes down to 0, but other implementations liek jythojn or pypy might do differently. Also if you have a long living function, then the with statement ensures, that the file is closed as soon as possible – gelonida Jan 07 '21 at 00:50
257

In Python 3.5 or later, using pathlib you can copy text file contents into a variable and close the file in one line:

from pathlib import Path
txt = Path('data.txt').read_text()

and then you can use str.replace to remove the newlines:

txt = txt.replace('\n', '')
Jonathan Sudiaman
  • 3,364
  • 1
  • 13
  • 15
  • 15
    This is so far the most elegant solution. I prefer to have a oneliner solution like R's read_file – Gang Su Sep 27 '20 at 16:17
  • When I use pathlib, I get this error `UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 148: character maps to `. And as far as I search, it seems I can't set encoding to pathlib, so I have to read the content traditionally with the os module. – Ali Akhtari Jun 22 '22 at 05:32
  • 1
    @AliAkhtari Per the docs, the signature is `read_text(encoding=None, errors=None)`. You should be able to pass the encoding there? – Jonathan Sudiaman Jul 07 '22 at 13:50
112

You can read from a file in one line:

str = open('very_Important.txt', 'r').read()

Please note that this does not close the file explicitly.

CPython will close the file when it exits as part of the garbage collection.

But other Python implementations won’t. To write portable code, it is better to use with or close the file explicitly. Short is not always better. See Is explicitly closing files important?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nafis Ahmad
  • 2,689
  • 2
  • 26
  • 13
  • 61
    This is anti-idiomatic and not recommended. `open` should be used within a `with ... as` statement. – Jorge Leitao Jan 09 '17 at 10:27
  • 2
    @J.C can you explain the problem ? Is this just a question of custom or does the `with ... as` statement bring something ? – Titou May 05 '17 at 12:08
  • 5
    @Titou the issue is that open.read() doesn't close the file so we either need `with ... as` or `str.close()` as demonstrated in Pedro's answer. More on the importance of closing files [here](http://stackoverflow.com/questions/7395542/is-explicitly-closing-files-important) – JBallin May 18 '17 at 21:01
  • @JBallin. This idiom clearly removes a source of error. Thanks ! – Titou May 19 '17 at 08:15
  • 16
    this is also bad because you've just shadowed `str()` from builtins – Chris_Rands Aug 13 '19 at 14:52
64

To join all lines into a string and remove new lines, I normally use:

with open('t.txt') as f:
  s = " ".join([l.rstrip("\n") for l in f]) 
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • 1
    It is giving UnicodeDecodeError in my code See this https://stackoverflow.com/q/18649512/9339242 – Arayan Singh Dec 12 '18 at 13:59
  • you may need to specify the character encoding. – Pedro Lobito Feb 11 '19 at 11:39
  • 1
    will remove trailing white space as well so perhaps better to `s = " ".join([l.replace("\n", "") for l in f]) ` – gelonida Jan 07 '21 at 01:00
  • @gelonida `rstrip` also supports specifying the charter(s) you want to remove, thanks for the tip. – Pedro Lobito Jul 09 '22 at 12:42
  • thanks as well. I forgot that `rstrip()` also has an argument. So your code is probably faster and operational except for some really weird cases where **one** line would contain a mix of trailing `" "` and `"\n"` like `"funny line \n \n "` – gelonida Jul 19 '22 at 16:35
35

Use:

with open("data.txt") as myfile:
    data = "".join(line.rstrip() for line in myfile)

join() will join a list of strings, and rstrip() without any arguments will trim whitespace, including newlines, from the end of strings.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MagerValp
  • 2,922
  • 1
  • 24
  • 27
19

There is also splitlines():

with open ("data.txt", "r") as myfile:
    data = myfile.read().splitlines()

Variable data is now a list that looks like this when printed:

['LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN', 'GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE']

Note there aren't any newlines (\n).

At that point, it sounds like you want to print back the lines to console, which you can achieve with a for loop:

for line in data:
    print(line)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Julian
  • 4,170
  • 4
  • 20
  • 27
  • 1
    That's because OP *didn't want* the output to be a list of strings, but a single string - at least as far as anyone could confirm. This answer would be suitable on https://stackoverflow.com/questions/12330522/how-to-read-a-file-without-newlines, but I'm pretty sure it's already well covered there. – Karl Knechtel Aug 09 '22 at 02:16
19

This can be done using the read() method:

text_as_string = open('Your_Text_File.txt', 'r').read()

Or as the default mode itself is 'r' (read) so simply use,

text_as_string = open('Your_Text_File.txt').read()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Loochie
  • 2,414
  • 13
  • 20
12

I have fiddled around with this for a while and have prefer to use use read in combination with rstrip. Without rstrip("\n"), Python adds a newline to the end of the string, which in most cases is not very useful.

with open("myfile.txt") as f:
    file_content = f.read().rstrip("\n")
    print(file_content)
gelonida
  • 5,327
  • 2
  • 23
  • 41
whirlwin
  • 16,044
  • 17
  • 67
  • 98
12

Here are four codes for you to choose one:

with open("my_text_file.txt", "r") as file:
    data = file.read().replace("\n", "")

or

with open("my_text_file.txt", "r") as file:
    data = "".join(file.read().split("\n"))

or

with open("my_text_file.txt", "r") as file:
    data = "".join(file.read().splitlines())

or

with open("my_text_file.txt", "r") as file:
    data = "".join([line for line in file])
My Car
  • 4,198
  • 5
  • 17
  • 50
  • An explanation would be in order. E.g., how are they different? Why are they different? What is the idea/gist? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing](https://stackoverflow.com/posts/71693542/edit), not here in comments (*** *** *** *** *** *** *** *** *** ***[without](https://meta.stackexchange.com/a/131011)*** *** *** *** *** *** *** *** *** *** *** *** *** *** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 08 '23 at 14:22
12

It's hard to tell exactly what you're after, but something like this should get you started:

with open ("data.txt", "r") as myfile:
    data = ' '.join([line.replace('\n', '') for line in myfile.readlines()])
Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
6

You can also strip each line and concatenate into a final string.

myfile = open("data.txt","r")
data = ""
lines = myfile.readlines()
for line in lines:
    data = data + line.strip();

This would also work out just fine.

OrionMD
  • 389
  • 1
  • 7
  • 13
Sai Kiriti Badam
  • 950
  • 16
  • 15
  • `data = data + line.strip();` can be reduced to `data += line.strip();` – Pedro Lobito Oct 05 '20 at 02:31
  • very inefficient for huge files (a lot of memory allocations and memory copies will take place. better to create list of stripped lines and then use " ".join()` – gelonida Jan 07 '21 at 01:04
6

You can compress this into one into two lines of code!

content = open('filepath', 'r').read().replace('\n', ' ')
print(content)

If your file reads:

hello how are you?
who are you?
blank blank

Python output

hello how are you? who are you? blank blank
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Michael Smith
  • 77
  • 1
  • 10
  • I like this solution as the last word of a line will be separated by a space from the first word of the next line. However I would suggest to use the `with` statement. So something like `with open("filepath", "r") as fin: content = fin.read().replace("\n", " ")` But if course it's nit sure whether this is needed by the original poster – gelonida Jan 07 '21 at 00:54
5
f = open('data.txt','r')
string = ""
while 1:
    line = f.readline()
    if not line:break
    string += line

f.close()


print(string)
gelonida
  • 5,327
  • 2
  • 23
  • 41
hungneox
  • 9,333
  • 12
  • 49
  • 66
  • 2
    Loops which have a `string += line` should be avoided. Some versions of Python may manage to avoid O(n^2) behaviour here but any of the other answers that have been given are better than this. Also you didn't remove the newlines that were requested so your code is just a very slow way of doing `string = f.read()` – Duncan Dec 03 '11 at 17:41
  • Thank for correcting me. But one small thing is that I have not to remove the new line, because when I tested, it didn't print '\n' out. @Duncan – hungneox Dec 03 '11 at 18:10
  • very inefficient for huge files. for every iteration memory has to be allocated and data has to be copied. Also: the new line is neither removed nor replaced with a " " Try to use following command to see, that the new lines are still contained. `print(repr(string))` – gelonida Jan 07 '21 at 00:58
  • An explanation would be in order. E.g., what is the idea/gist? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/8369232/edit), not here in comments (but *** *** *** *** *** *** *** *** *** *** *** *** ***[without](https://meta.stackexchange.com/a/131011)*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 08 '23 at 15:14
4

Python 3: See List Comprehensions for the square bracket syntax.

 with open('data.txt') as f:
     lines = [ line.strip('\n') for line in f ]
gerardw
  • 5,822
  • 46
  • 39
  • Very pythonic and worked for me quite well, although I haven't tested on large files yet. Thank you! – stux Jul 09 '20 at 17:41
  • I'm going to be retracting my upvote because strip also strips whitespace, which may not be the desired behavior. However, I still think a modified version of this would be good. – stux Jul 09 '20 at 19:37
  • `lines = list(map(str.strip, f))`? – xtofl Apr 22 '21 at 12:28
4

This is a one line, copy-pasteable solution that also closes the file object:

_ = open('data.txt', 'r'); data = _.read(); _.close()
Edward D'Souza
  • 2,453
  • 1
  • 26
  • 24
4

Oneliner:

  • List: "".join([line.rstrip('\n') for line in open('file.txt')])

  • Generator: "".join((line.rstrip('\n') for line in open('file.txt')))

List is faster than generator but heavier on memory. Generators are slower than lists and is lighter for memory like iterating over lines. In case of "".join(), I think both should work well. .join() function should be removed to get list or generator respectively.

  • Note: close() / closing of file descriptor probably not needed
Machinexa
  • 626
  • 1
  • 8
  • 17
3

Try this:

x = "yourfilename.txt"
y = open(x, 'r').read()

print(y)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
3

To remove line breaks using Python you can use replace function of a string.

This example removes all 3 types of line breaks:

my_string = open('lala.json').read()
print(my_string)

my_string = my_string.replace("\r","").replace("\n","")
print(my_string)

Example file is:

{
  "lala": "lulu",
  "foo": "bar"
}

You can try it using this replay scenario:

https://repl.it/repls/AnnualJointHardware

enter image description here

Sma Ma
  • 3,343
  • 2
  • 31
  • 39
3

Use:

from pathlib import Path
line_lst = Path("to/the/file.txt").read_text().splitlines()

It is the best way to get all the lines of a file. The '\n' are already stripped by the splitlines() (which smartly recognize win/mac/unix lines types).

But if nonetheless you want to strip each lines:

line_lst = [line.strip() for line in txt = Path("to/the/file.txt").read_text().splitlines()]

strip() was just a useful exemple, but you can process your line as you please.

At the end, do you just want concatenated text?

txt = ''.join(Path("to/the/file.txt").read_text().splitlines())
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yota
  • 2,020
  • 22
  • 37
2

I don't feel that anyone addressed the [ ] part of your question. When you read each line into your variable, because there were multiple lines before you replaced the \n with '' you ended up creating a list. If you have a variable of x and print it out just by

x

or print(x)

or str(x)

You will see the entire list with the brackets. If you call each element of the (array of sorts)

x[0] then it omits the brackets. If you use the str() function you will see just the data and not the '' either. str(x[0])

John Galbraith
  • 387
  • 1
  • 4
2

You could try this. I use this in my programs.

Data = open('data.txt', 'r')
data = Data.readlines()
for i in range(len(data)):
    data[i] = data[i].strip() + ' '
data = ''.join(data).strip()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

A regular expression works too:

import re
with open("depression.txt") as f:
     l = re.split(' ', re.sub('\n',' ', f.read()))[:-1]

print (l)

Output:

['I', 'feel', 'empty', 'and', 'dead', 'inside']

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex
  • 944
  • 4
  • 15
  • 28
2
with open('data.txt', 'r') as file:
    data = [line.strip('\n') for line in file.readlines()]
    data = ''.join(data)
  • An explanation would be in order. E.g., what is the idea/gist? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/68542399/edit), not here in comments (*** *** *** *** *** ***[without](https://meta.stackexchange.com/a/131011)*** *** *** *** *** *** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 08 '23 at 14:25
1

This works: Change your file to:

LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE

Then:

file = open("file.txt")
line = file.read()
words = line.split()

This creates a list named words that equals:

['LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN', 'GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE']

That got rid of the "\n". To answer the part about the brackets getting in your way, just do this:

for word in words: # Assuming words is the list above
    print word # Prints each word in file on a different line

Or:

print words[0] + ",", words[1] # Note that the "+" symbol indicates no spaces
#The comma not in parentheses indicates a space

This returns:

LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN, GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE
PyGuy
  • 49
  • 3
  • 1
    Changing the file might work in a one off situation but if you have hundreds of files this just isnt a workable solution. – Craicerjack Mar 03 '17 at 14:57
0

Use:

with open(player_name, 'r') as myfile:
    data = myfile.readline()
    list = data.split(" ")
    word = list[0]

This code will help you to read the first line and then using the list and split option, you can convert the first line word separated by space to be stored in a list.

then you can easily access any word, or even store it in a string.

You can also do the same thing with using a for loop.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0
file = open("myfile.txt", "r")
lines = file.readlines()
str = ''                                     #string declaration

for i in range(len(lines)):
    str += lines[i].rstrip('\n') + ' '

print str
akD
  • 1,137
  • 1
  • 10
  • 15
  • An explanation would be in order. E.g., what is the idea/gist? From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/37785427/edit), not here in comments (but *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***[without](https://meta.stackexchange.com/a/131011)*** *** *** *** *** *** *** *** *** *** *** *** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 08 '23 at 14:48
-3

Try the following:

with open('data.txt', 'r') as myfile:
    data = myfile.read()

    sentences = data.split('\\n')
    for sentence in sentences:
        print(sentence)

Caution: It does not remove the \n. It is just for viewing the text as if there weren’t any \n.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Palak Jain
  • 664
  • 6
  • 19