Python Forum
Substitution with regular expression returns hidden character SOH
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Substitution with regular expression returns hidden character SOH
#1
Hello community, hope everyone is having a good time.

I'm doing a pretty easy script, look for a certain format of values in a file then repeat them three times in an output file.

Example:
Find -> 0.875*V
Substituted -> =0.875*V<TAB>=0.875*V<TAB>=0.875*V<TAB>

This regular expression already has worked us in Perl but I have not been able to figure out why it fails in Python.

From the results I can see the group is captured correctly but it is substituted with SOH character(Start Of Header) instead of captured value.

Our work environment:
- Windows 10 Pro
- Python 3.8

import re
from pathlib import Path

FILEHANDLER_R = open( "input.txt", "r" )
FILEHANDLER_W = open( "output.txt", "w" )

for line in FILEHANDLER_R:
	line = re.sub( "\t(\d*\.*\d*\**V*)", "\t=\1\t=\1\t=\1", line )
	print( line )
	FILEHANDLER_W.write( line )
	

FILEHANDLER_R.close()
FILEHANDLER_W.close()
Input file is:

Quote:vdd_0p9_hsio_ebipll 0.875*V 0.875*V 0.920*V 1.1*V 1.1*V 0.830*V 0.830*V 0.830*V 0.875*V


Output file results:
Output:
vdd_0p9_hsio_ebipll = = = = = = = = = = = = = = = = = = = = = = = = = = =
Any ideas you can share with us is greatly appreciated.

Thanks for the time reading this post.
Reply
#2
>>> s = '\t=\1'
>>> s
'\t=\x01'
>>> print(s)
	=
>>> 
>>> # Fix
>>> s = r'\t=\1'
>>> s
'\\t=\\1'
>>> print(s)
\t=\1
Always use raw string r with regex because of escape characters.
>>> import re
>>> 
>>> data = 'vdd_0p9_hsio_ebipll	0.875*V	0.875*V	0.920*V	1.1*V	1.1*V	0.830*V	0.830*V	0.830*V	0.875*V'
>>> line = re.sub(r"\t(\d*\.*\d*\**V*)", r"\t=\1\t=\1\t=\1", data)
>>> print(line)
Output:
vdd_0p9_hsio_ebipll =0.875*V =0.875*V =0.875*V =0.875*V =0.875*V =0.875*V =0.920*V =0.920*V =0.920*V =1.1*V =1.1*V =1.1*V =1.1*V =1.1*V =1.1*V =0.830*V =0.830*V =0.830*V =0.830*V =0.830*V =0.830*V =0.830*V =0.830*V =0.830*V =0.875*V =0.875*V =0.875*V
Reply
#3
Hi snippsat.

Thank you for your answer, straight to the point.

I definitely need to read about these raw strings.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  data validation with specific regular expression shaheen07 0 323 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Copy only hidden files and folders with rsync Cannondale 2 997 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  Regular Expression search to comment lines of code Gman2233 5 1,658 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,660 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Need help with my code (regular expression) shailc 5 1,916 Apr-04-2022, 07:34 PM
Last Post: shailc
  Can ZipFile be used to extract hidden files? AiedailEclipsed 0 1,615 Mar-22-2022, 05:21 PM
Last Post: AiedailEclipsed
  Regular Expression for matching words xinyulon 1 2,164 Mar-09-2022, 10:34 PM
Last Post: snippsat
  regular expression question Skaperen 4 2,475 Aug-23-2021, 06:01 PM
Last Post: Skaperen
  How can I find all combinations with a regular expression? AlekseyPython 0 1,666 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  [solved] unexpected character after line continuation character paul18fr 4 3,388 Jun-22-2021, 03:22 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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