简体   繁体   中英

Python readline reading incorrect \n

I am trying to process a bunch of files; my test set is 50 files; 47 of the files work fine but 3 of the files have a weird issues. Once this is working I can process the few thousand other files.

I am trying to create a more useful set of files; by default the files are named with a number based on when they are created; the "pretty" name is in the first line; it is bracketed by a bunch of hex which for my purposes is useless; the beginning of the name is always at 0x80 and character following the end of the name is always 0x00

with open(i, 'rb') as f:
    firstline = f.readline()
    #print(str(firstline))
    posBegin=128
    posEnd=firstline.find(hexNameEnd.encode(),posBegin + 1)

In one of the files; printing firstline gives;

 ...x19\x01\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00\x19\x01\x00\x00\n

But looking at the file in a hex editor shows only 0A in this position; how do I force it to look only for windows line endings; 0D 0A? Adding newline='\r\n' doesn't work.

Ok so obviously I am now feeling dumb; my issue is that I was using readline() rather then read()

with open(i, 'rb') as f:
    firstline = f.read(200)
    #print(str(firstline))
    posBegin=128
    posEnd=firstline.find(hexNameEnd.encode(),posBegin + 1)

Since the name is always at 0x80; and the names are never any longer then approx 30 characters; reading the first 200 bytes is all I needed to do. Grab my data from there. Even if I get some really long names they should easily fit within the read bytes.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM