When I tried to remove the line breaks in the file using python, I got stuck because I didn't understand the specifications for reading the file. This article is more about "reading a file" than "erasing line breaks". I'm not sure if this method is good because it is written by a python beginner. Please read it for reference only.
First I will give you the wrong example.
test1.py
#!/usr/bin/env python
#coding:utf-8
f = open('test1.txt','r')
for line in f:
text = line.replace('\n','')
text = text.replace('\r','')
print text,
f.close()
What if I do this?
Execution result
test test
I should have replaced the line feed code with an empty string, but there was a space. This is because ~~ ** is processing line by line, so ** is doing the same thing as the code below ~~. (There seems to be a way to erase blanks even if processing is performed line by line from the comment, so there seems to be a method to process line by line. (Corrected on 6/15))
space.py
#!/usr/bin/env python
#coding:utf-8
print 'test',
print 'Tesuto',
print 'test'
Line breaks can be removed by ,
at the end of the sentence, but spaces are also inserted in this case.
In the case of python, if you process each line, a space will be inserted for each process even if it is not in the text.
To solve this, let's read and process all the files instead of line by line.
Use read ()
to read the entire file.
test2.py
#!/usr/bin/env python
#coding:utf-8
f = open('test1.txt','r')
Allf = f.read()
text = Allf.replace('\n','')
text = text.replace('\r','')
print text,
f.close()
Execution result
test test
I was able to safely take line breaks and spaces.
If you do not enter # coding: utf-8
at the beginning of the line, a Syntax Error may occur when you write a line feed code in the code.
If you get an error such as Non-ASCⅡ, check the declaration.
The line feed code is as follows.
system | Line feed code |
---|---|
Linux | \n |
Mac | \r |
Windows | \r\n |
In many cases, you may be able to go with just \ n, but in the case of Windows etc., you need to remove \ r as well, so be careful.
Recommended Posts