I’ve a file which consists of very long string about 300 over character set of string. The string consists of space and new line characters.
The content of the file is as follow:
[EEPROM]
Logic EEPROM = 67 0 0 14 1 249 95 15 65 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 51 69 48 48 56 1 48 52 49 0 48 54 4 67 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 48 8 49 50 50 9 51 48 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 48 48 48 48 48 0 4 0 0 22 145 38 51 49 5 53 51 49 52 5 5 52 55 0 48 4 57 48 45 55 48 54 57 51 0 0 0 0 0 0 0 0 0 0 0 1 0 2 0 0 48 53 45 4 54 45 50 48 9 55 48 53 45 49 5 45 50 48 49 55 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Data EEPROM = 140 0 0 226 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 3 1 1 0 0 0 0 1 219 2 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 1 1 1 0 0 0 1 219 2 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 1 1 0 0 0 0 1 219 2 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 219 2 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 1 1 0 1 50 0 1 219 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 1 1 0 0 0 0 1 19 2 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 18 1 1 0 0 0 0 1 19 2 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I want to extract the data of "Logic EEPROM". I’ve tried to test with the following python code.
LogicEE_St = 'Logic EEPROM = '
fileToRead = 'C:\Users\Arduino\Desktop\Python\Python_NSE\Test.txt'
with open(fileToRead, 'r') as fp:
# read all lines in a list
lines = fp.readlines()
for line in lines:
# check if string present on a current line
if line.find(LogicEE_St) != -1:
LogicEE_Ref_Line_1 = line
break
print(LogicEE_Ref_Line_1)
split_text = LogicEE_Ref_Line_1.replace('n', '').replace('r', '').replace(' n', '').replace(' r', '')
print(split_text)
split_text = LogicEE_Ref_Line_1.strip()
print(split_text)
print("n".join(item for item in LogicEE_Ref_Line_1.split('n') if item))
clean_text = "".join(split_text)
print(clean_text)
The result is not coming out with the one line. It is coming with new line character.
I want to remove new line character only so that the string does not have new line character.
I’ve searched the web and try everything I can but it still does not work.
By the way, I use visual studio code to test and notepad++ and sublime text (without word wrap) to see the result as one line.
Best regards,
Aung KL
2
Answers
use
.rstrip("n")
and iterate by-lines instead!when you
open()
a file in text mode (this is the default, rather than binaryb
) it’ll become anio.TextIOWrapper()
TextIOWrapper
dynamically replaces platform-specific newlines (such as the wacky Windowsrn
) with justn
when presented to your programfor line in fh: ...
, avoiding.readlines()
As you read each line, remove all/any trailing whitespace as follows: