There is a script:

def extract(filePath): f = open(filePath, 'rb') i = 0 lines = [] for line in f: print(line) line = str(line) if line == 'PlAr\n': break lines.append(line.split('\t')) i += 1 return lines[13:len(lines)-1] 

The problem is that when line is converted to a line, the letter "b" appears at the beginning of each line, for example, the line in the file was 'text', and after the conversion it became 'b "text"'. In addition to the character b, quotation marks also appear. In general, how to make the string converted without a character and quotes?

    3 answers 3

    You open in binary read mode ( rb ), so bytes are returned, they have literal b'' , and when you apply str to bytes, you get a textual representation of this type.

    Solutions:

    • Open in text reading mode:

       f = open(filePath, 'r') 

      But, you may need to specify the file encoding, for example:

       f = open(filePath, 'r', encoding='utf8') 
    • Either leave it as it is and translate from bytes to a string, for example:

       line = str(line, encoding='utf-8') 

      or so:

       line = line.decode('utf-8') 
    • It turned out only with the indication of the encoding. - Sonic Myst

    Open file as text, not binary

     f = open(filePath, 'r') 
    • Initially this was done when working on Windows, but on Linux I get an error - Sonic Myst

    Decode the resulting string:

     line.decode('utf-8') # можно и просто line.decode() по умолчанию utf-8 

    or open the file as a text 'r', not a binary 'rb'.