skip to Main Content

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


  1. use .rstrip("n") and iterate by-lines instead!

    when you open() a file in text mode (this is the default, rather than binary b) it’ll become an io.TextIOWrapper()

    • TextIOWrapper dynamically replaces platform-specific newlines (such as the wacky Windows rn) with just n when presented to your program
    • and is already an iterable by-lines for line in fh: ..., avoiding .readlines()
    with open(path_file) as fh:
        for line in fh:
            if line.startswith("Logic EEPROM "):
                print(repr(line.rstrip("n")))
                break  # no need to read any more lines
        else:  # didn't find and break
            raise Exception(f"failed to read Logic EEPROM from {path_file}")
    
    >>> from io import StringIO  # hack to make text act like a file here
    >>> data = StringIO("""[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
    ... """)
    >>> for line in data:
    ...     if line.startswith("Logic EEPROM "):
    ...         print(repr(line.rstrip("n")))
    ... 
    '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'
    
    Login or Signup to reply.
  2. As you read each line, remove all/any trailing whitespace as follows:

    LogicEE_St = 'Logic EEPROM = '
    
    fileToRead = 'foo.txt'
    
    with open(fileToRead) as fp:  
        for line in map(str.rstrip, fp):
            if line.startswith(LogicEE_St):
                # line will not have any trailing newline character(s)
                print(line)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search