Line extracted from text not matching the content of the text array
Question:
I have this block of code:
def removal(i):
with open(r"D:LargecodefileCustomer.txt", "r") as f:
lines = f.readlines()
h,j = i-2,i-1
data1, data2 = lines[h],lines[j]
print('data is ',data1,'and',data2)
print('line is ',lines)
with open(r"D:LargecodefileCustomer.txt", "w") as f:
for line in lines:
if line.strip('n') != data1 and data2:
f.write(line)
I’m trying to make it rewrite the file and exclude the specific line that I choose. As of right now, my line output is:
line is ['JSJSJSJSJSn', '10-02-1975n', 'HAHAHAHAHAn', '26-11-2001n', 'BOTBOITBOTn', '13-09-2018n', 'Philln', 'Gill']
and data1 and data 2 is:
data is Philln and Gill
They seem matchup to the list from I what see, however, the system is not recognizing it, I think the problem is at the second last line of the code but I can’t make it work. Is it that I went wrong somewhere? Thank you!
Edit: Hi, it’s me again, I just want to add that I’ve tried using 1 data to excluded it too and see if it works, if line.strip('n') != data1:
but sadly the result is still the same, I tried removing n either, not reccomended but I did it anyway, does not get affected too
Answers:
It’s much easier if you strip the incoming file as you build a list of lines. splitlines on a string is ideal for this.
If you use open mode r+ there’s no need to open the file twice. Just seek to BOF after consuming the file content and remember to call truncate before closing.
def removal(i):
with open(r"D:LargecodefileCustomer.txt", 'r+', newline='') as f:
lines = f.read().splitlines()
h, j = i-2, i-1
# validation here is critical
if h >= 0 and j < len(lines):
data1, data2 = lines[h], lines[j]
f.seek(0) # BOF
for line in lines:
if not line in {data1, data2}:
print(line, file=f) # print will add the newline
f.truncate()
Thats a syntax mistake. You are checking if line does not equal to data1, and if data2 is True:
if line.strip('n') != data1 and data2:
solution (check if line also does not equal to data2):
if line != data1 and line != data2:
I have this block of code:
def removal(i):
with open(r"D:LargecodefileCustomer.txt", "r") as f:
lines = f.readlines()
h,j = i-2,i-1
data1, data2 = lines[h],lines[j]
print('data is ',data1,'and',data2)
print('line is ',lines)
with open(r"D:LargecodefileCustomer.txt", "w") as f:
for line in lines:
if line.strip('n') != data1 and data2:
f.write(line)
I’m trying to make it rewrite the file and exclude the specific line that I choose. As of right now, my line output is:
line is ['JSJSJSJSJSn', '10-02-1975n', 'HAHAHAHAHAn', '26-11-2001n', 'BOTBOITBOTn', '13-09-2018n', 'Philln', 'Gill']
and data1 and data 2 is:
data is Philln and Gill
They seem matchup to the list from I what see, however, the system is not recognizing it, I think the problem is at the second last line of the code but I can’t make it work. Is it that I went wrong somewhere? Thank you!
Edit: Hi, it’s me again, I just want to add that I’ve tried using 1 data to excluded it too and see if it works, if line.strip('n') != data1:
but sadly the result is still the same, I tried removing n either, not reccomended but I did it anyway, does not get affected too
It’s much easier if you strip the incoming file as you build a list of lines. splitlines on a string is ideal for this.
If you use open mode r+ there’s no need to open the file twice. Just seek to BOF after consuming the file content and remember to call truncate before closing.
def removal(i):
with open(r"D:LargecodefileCustomer.txt", 'r+', newline='') as f:
lines = f.read().splitlines()
h, j = i-2, i-1
# validation here is critical
if h >= 0 and j < len(lines):
data1, data2 = lines[h], lines[j]
f.seek(0) # BOF
for line in lines:
if not line in {data1, data2}:
print(line, file=f) # print will add the newline
f.truncate()
Thats a syntax mistake. You are checking if line does not equal to data1, and if data2 is True:
if line.strip('n') != data1 and data2:
solution (check if line also does not equal to data2):
if line != data1 and line != data2: