My goal is to take two text files, read them in, identify lines in file A that need to replace the same lines in file B and be saved as file C.
I am getting stuck on how to get the specific lines from file A. I need the whole line - my searchable string is essentially the first X characters, and always starts with a :.
from PyQt4.QtGui import *
searchable_strings = (':ABC',':123',':DEF')
entries_from_a = []
app = QApplication( sys.argv )
with open( unicode ( QFileDialog.getOpenFileName() )) as file_a:
readlines_a = file_a.read()
file_a.closed
with open( unicode ( QFileDialog.getOpenFileName() )) as file_b:
readlines_b = file_b.read()
file_b.closed
app.quit()
for line in readlines_a:
for item in searchable_strings:
if item in line:
entries_from_a.append(line)
On a side note, every single time I run it, I have needed to quit Spyder and open a new window to continue due to this error
QWidget: Must construct a QApplication before a QPaintDevice
The reason you not getting anything in entries_from_a
is because for line in readlines_a
reads one character at a time. And all your searchable_strings
are more then 1 character long.
Here is an example:
echo "aaa\nbbb\n\ccc" > foo.txt
>>> with open('foo.txt') as f:
... my_data = f.read()
...
>>> for i in my_data:
... print i
...
a
a
a
\
n
b
# and so on
So what you probably looking for is the way to iterate over each line.
Like @Vor said you are not iterating over lines in your for loop, but over the characters of a string. file_a.read()
returns the whole file as one string. Using for instance readlines_a = file_a.readlines()
would return a list of the lines of file_a
. So would using a list comprehension, which is probably the faster option and better if your files are large. Then your for loop would iterate over lines as you intend.
with open( unicode ( QFileDialog.getOpenFileName() )) as file_a:
readlines_a = file_a.readlines() # readlines() method
with open( unicode ( QFileDialog.getOpenFileName() )) as file_a:
readlines_a = [line for line in file_a] # list comprehension
then your for loop
Alternatively:
with open('filefoo') as file_a:
with open('filebar') as file_b:
for line in file_a:
etc.
You don't need to call the file's readlines
method, since the file object is an iterable object.
with open( unicode ( QFileDialog.getOpenFileName() )) as f:
readlines_a = [line for line in f]
print readlines_a
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.