Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error when using FIND
#1
I am trying to find a string in a text file and get an error on the first find statement.
            xrefstart=irec.find("\x - \xo ")
            if xrefstart > 0:
                  #find the \xt which is the start of the xref
                  xstar=irec.find("x*")
                  xref = "<RX>"+irec[chr+10:xstar]+"<Rx>"
                  # replace everything up to the "x* " with xref
                  regexp = "\x - \xo .*?\x* "
                  re.sub(regexp, xref, irec)
The error reads:
(unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape

How do I correct this?
"irec" contains:
\v 1 \x - \xo 1:1 \xt Ps 90:2; Jes 40:21-22; Joh 1:1-3; Hand 17:24; Kol 1:16-17; Heb 1:10; 11:3\x*In die begin het God die hemel en die aarde geskep.
Reply
#2
Hi,

I guess something went wrong before. \x marks the start of a hex sequence and Python expacts that two characters from the 0-9a-h range follow. Which is not that case in your code.

However, the questions is why your string includes hex squences anyway. I looks like an extract from a bible or something like that and they rarely used hex sequences in their texts back in the days. I guess when reading / getting the data which lead to the string something was wrong with encodng applied?

Gruß, noisefloor
Reply
#3
What is irec? Is it str or bytes? If it is bytes, your search pattern must also be bytes. Either way you should use a raw literal for the pattern to prevent "\x" being interpreted as an escape sequence marking the start of a hexadecimal number.
xrefstart=irec.find(r"\x - \xo ")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error in find pearson correlation function erneelgupta 1 1,810 Mar-01-2022, 03:41 PM
Last Post: stevendaprano
  Getting 'NoneType' object has no attribute 'find' error when WebScraping with BS Franky77 2 5,166 Aug-17-2021, 05:24 PM
Last Post: Franky77
  List of error codes to find (and count) in all files in a directory tester_V 8 3,585 Dec-11-2020, 07:07 PM
Last Post: tester_V
  Cant find root cause of thread.lock error burlyboys 0 1,523 May-18-2020, 12:51 PM
Last Post: burlyboys
  How to ignore - ERROR: The system was unable to find the specified registry key or va asheru93 9 6,550 Feb-04-2019, 06:35 AM
Last Post: asheru93
  Can't find error in code but Python throws exception Sandwich_masterX 3 2,865 Oct-09-2018, 01:38 AM
Last Post: ichabod801
  subprocess error : The system cannot find the file specified evilcode1 0 10,659 Oct-03-2018, 08:04 AM
Last Post: evilcode1
  Getting error str object is not callable in find by xpath sumandas89 3 10,669 Aug-14-2018, 03:05 PM
Last Post: mlieqo
  Windows cannot find "wish.exe" - error while trying to run PAGE Nwb 0 7,509 Jun-11-2018, 12:08 PM
Last Post: Nwb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020