re.sub and re.subn [duplicate] - python

This question already has answers here:
How to check if re.sub() has successfully replaced in python? [duplicate]
(2 answers)
Python: how to substitute and know whether it matched
(3 answers)
Closed 2 years ago.
I want to use re.subn in my program to get no of times changes are made but I can't figure it out how to write the code. I have written the program with re.sub and it works fine but If I use (re.sub(r'\bi\b','I',contents)) in this program it shows this error.
TypeError: write() argument must be str, not int
import re
with open ('Sample.txt','r') as rf:
contents=rf.read()
change=(re.sub(r'\bi\b','I',contents))
with open ('Sample2.txt','w') as wf:
wf.writelines(change)
Can someone please guide how can I use re.subn to get the count.
Thank you.

Related

String pattern in Python [duplicate]

This question already has answers here:
How do I validate a date string format in python?
(5 answers)
Closed 6 months ago.
Im trying to check if a user's input is following the pattern integer/integer/integer(like month/day/year) but i dont know how to use exactly the match function to define that the pattern contains "number",then "/",again "number" and "/"...
Check out https://regex101.com/ for a neat website to check your regex! This is implemented in python using the re library. https://docs.python.org/3/library/re.html
In your case, the pattern would be [0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}

What is the Python ` symbol [duplicate]

This question already has answers here:
What do backticks mean to the Python interpreter? Example: `num`
(3 answers)
Meaning of the backtick character in Python
(2 answers)
Closed 1 year ago.
Lots of old python code I look in has this ` symbol around a lot of stuff, what does it do? Now it is not considered valid syntax, obviously.
And I don't think it is just another string identifier, its sometimes wrapped around functions in the code I'm looking at.
Any help will be appreciated.

Explanation or documentation of how this string replace lambda group function works [duplicate]

This question already has answers here:
Function to dictate the replacements in re.sub method (Python)
(2 answers)
How to input a regex in string.replace?
(7 answers)
Closed 2 years ago.
I have this function here which I use to replace the a string like this 'ABCDe.CO' with 'ABCD-E.CO' in a pandas dataframe.
I don't understand how the group(0) part works, I can't find any documentation on it. Could someone explain to me what this function does or where I can read up on it?
(df.loc[df.country.eq('ST'), 'ticker'].str.replace('([a-z])', lambda x: '-'+x.group(0).upper()))

pythonic way to write the code for search and replace string [duplicate]

This question already has answers here:
Search and replace operation
(2 answers)
Closed 6 years ago.
I have written code to search and replace string in make command as per user input
make language='english' id=234 version=V1 path='/bin'
In above code i searched version=V1 and replace version with version=V2
import re
strings = "make language='english' id=234 version=V1 path='/bin'"
search_pattern= re.search('version=(.*?)\s', strings)
old_str = search_pattern.group(1)
print test.replace(old_str, "V2")
Can anyone help me write above code in pythonic way or any other way to write above code
It's very easy if you use str.replace
String = "make language='english' id=234 version=V1 path='/bin'"
String = String.replace("version=V1", "version=V2")

How to get help on methods in Python? [duplicate]

This question already has answers here:
python: how to get information about a function?
(5 answers)
Closed 8 years ago.
whenever I use help() on a method I get a weird looking output.
For example, help(obj.readline) gives me the following output:
Help on built-in function readline:
readline(...)
That's all i get. I don't know how to interpret it.Why there is no description about the method? Can somebody please explain to me what's going on?
Is this weird output?
>>> f = open('data.txt')
>>> help(f.readline)
Help on built-in function readline:
readline(...)
readline([size]) -> next line from the file, as a string.
Retain newline. A non-negative size argument limits the maximum
number of bytes to return (an incomplete line may be returned then).
Return an empty string at EOF.

Categories

Resources