urls = [] url = "" state = "start" for b in open("labels-with-urls.txt", "rb").read(): if state == "start" and b == "\x0A": state = "url" elif state == "url": if b in "\x0A\x0D": state = "start" url = "" elif b == "\x00": state = "appendurl" else: url += b elif state == "appendurl": if b == "\x00": state = "start" urls.append(url) url = "" else: state = "start" url = "" urlfile = open("labels-urls.txt", "wb") for url in urls: urlfile.write(url + "\n")