Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have couple of files with like this, llm_rc_v3212.xml, llm_ds_v3232.xml.
Names can be anything. however, common parameter would be_v3212. I want to match this number and replace it (ideally renaming the file).
How can i match this pattern with regex? I am trying to use re.sub, but not able to figure yet.
any help would be appreciated.
Here is a working example. Take into account that depending on other filenames the regex might need to be changed.
import re
FILENAME_VERSION_REGEX = re.compile(r'_v(\d)+')
def rename(filename, replacement):
full_replacement = r'_v{}'.format(replacement)
new_filename = FILENAME_VERSION_REGEX.sub(full_replacement, filename)
return new_filename
Tested with the filenames you gave:
>>> rename('llm_rc_v3212.xml', 1)
'llm_rc_v1.xml'
>>> rename('llm_ds_v3232.xml', 2)
'llm_ds_v2.xml'
>>> rename('llm_v232_uc.xml', 3)
'llm_v3_uc.xml'
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 months ago.
Improve this question
How can I replace double quotes inside single quoted sections with something like \"?
Given this text:
{['abc.abc',"dsa",asd:'<td id="ssa" width="2px" class="odd-column">']}
I want it to be:
{['abc.abc',"dsa",asd:'<td id=\"ssa\" width=\"2px\" class=\"odd-column\">']}
In a later step I will replace all single quotes to get proper json for convertion.
Here is a python script which does what you are asking for.
We have to escape all the " and \
import re
string = r"""{['abc.abc',"dsa",asd:'<td id="ssa" width="2px" class="odd-column">']}"""
print(string)
sstring = re.sub(r'\"','\\"',string)
print(sstring)
the output is
{['abc.abc',\"dsa\",asd:'<td id=\"ssa\" width=\"2px\" class=\"odd-column\">']}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I've got an issue where my regex isn't parsing the output of a file I created:
#!/usr/bin/env python3
import wget, re
url=''
filename=wget.download(url)
with open ('Output.txt', "r") as f:
readlines=f.read()
ret=re.sub("^.*\^", "", readlines)
print(ret)
According to this site, the regex I'm using "^.*\^" is valid for my output. Sample output I'm feeding it is something like this:
1212-2010^readthispart
Where it has a carot for a delimiter. I tried double and single quotes to no avail and I'm not sure if it's an issue elsewhere in my code or what, but the printout does not match what I'm looking for. Ideas?
If I'm reading your question and edits right you're looking to return 'readthispart', correct? If so you need to look into using look-behinds in combination with search. See https://docs.python.org/2/library/re.html. re.search("(?<=\^).*",myinput)
You need to enable multiline mode:
re.sub('^.*\^', '', readlines, flags=re.MULTILINE)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a question that states
Write a function fcopy() that takes as input two file names (as strings) and copies the content of the first file into the second.
and I want to know how to go about solving this.
My first file is named example, and the second file is named output, both text files are in .txt format, and the path to them are
"C:\Users\HOME\Desktop\Introduction to Computing\Lab\assignments\example.txt"
and "C:\Users\HOME\Desktop\Introduction to Computing\Lab\assignments\output.txt"
You are not to ask StackOverflow to do your homework for you. Feeling generous though...
First of all, read this: https://docs.python.org/3.3/library/shutil.html It's the Python 3 docs for the shutil module. It will give high-level functions for reading/writing files (I/O).
from shutil import copyfile
copyfile(locationOfSource, locationOfDestination)
An important thing to note is that "\" (back-slash) signifies non-literal text, so "\n" means new line, NOT just "\n". This is rarely mentioned and had me stumped when I first learnt escape characters. To do the back-slash that you want within a string, you MUST use "\" instead of "\".
The commenters below your answer are correct, please read the information given to you by StackOverflow about asking questions. Also, welcome to the site.
If you really need to, you could write a simple wrapper function to accomplish this:
def copy_file(orig_file_name, copy_file_name):
with open(orig_file_name, 'r') as orig_file, open(copy_file_name, 'w+') as cpy_file:
orig_file = orig_file.read()
cpy_file.write(orig_file)
But as #Frogboxe has already said, the correct way to copy a file is to used the shutil library:
import shutil
shutil.copy(target_file, copy_file)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a huge text file, each line has a tab-delimited string. I need to keep all tabs apart from those at the end of each line. I need to keep the carriage return. Any ideas?
I've tried everything on these answers:
How to trim whitespace (including tabs)?
Trimming a string in Python
Strip spaces/tabs/newlines - python
as well as others I've now closed the tabs on.
Just use a regular expression
>>> import re
>>> s="1\t2\t3\t\t\n"
>>> s2=re.sub('\t+\n','\n',s)
>>> s2
'1\t2\t3\n'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
for some reason when I get regex to get the number i need it returns none.
But when I run it here http://regexr.com/38n3o it works
the regex was designed to get the last number of the ip so it can be removed
lanip=74.125.224.72
notorm=re.search("/([1-9])\w+$/g", lanip)
That is not how you define a regular expressions in Python. The correct way would be:
import re
lanip="74.125.224.72"
notorm=re.search("([1-9])\w+$", lanip)
print notorm
Output:
<_sre.SRE_Match object at 0x10131df30>
You were using a javascript regex style. To read more on correct python syntax read the documentation
If you want to match the last number of an IP use:
import re
lanip="74.125.224.72"
notorm=re.search("(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", lanip)
print notorm.group(4)
Output:
72
Regex used from http://www.regular-expressions.info/examples.html
Your example did work in this scenario, but would match a lot of false positives.
What is lanip's type? That can't run.
It needs to be a string, i.e.
lanip = "74.125.224.72"
Also your RE syntax looks strange, make sure you've read the documentation on Python's RE syntax.