A fairly simple question, but for the life of me I can't figure it out...
I have the following piece of code:
os.rename(FileName, FileName.replace(".png","")+"_"+time_stamp.replace(":","-").replace(" ","_")+"_"+name_string+".png")
Basically renaming an image file.
I want to use the .format() structure, but I can't figure out how to get the .replace() function to work with this.
At the moment my converted code looks like this:
os.rename(FileName, "{}.replace(".png","")_{}.replace(":","-").replace(" ","_")_{}.png".format(FileName,time_stamp,name_string))
At the moment the error being given is "invalid syntax" at the first replace ( a caret at the "" in (".png","")).
Could someone kindly point me in the right direction for resources to help with this?
Is there a better way of doing what I'm trying to?
Thanks
You shouldn't mix python string operation replace() with arguments for format().
os.rename(FileName, '{0}_{1}_{2}.png'.format(
FileName.replace(".png", ""),
time_stamp.replace(":", "-"),
name_string.replace(" ", "_")
))
The basic form of the format you're using is this:
"Hello {}".format("there")
which evaluates to output string
"Hello there"
In its simple form, each {} in your string will get replaced by the strings in format, in order. More detailed info, you can find here:
https://pyformat.info/
what are you need to do is
os.rename(FileName, "{0}_{1}_{2}".format(FileName.replace(".png",""),
time_stamp.replace(":","-").replace(" ","_"),
name_string+".png"))
or more readable
newName = "{rmvFileFormat}_{time}_{addFileFormat}".format(rmvFileFormat = FileName.replace(".png",""),
time = time_stamp.replace(":","-").replace("","_"),
addFileFormat = name_string+".png")
os.rename(FileName, newName)
replace is a string method, you can't use it inside the string you're formatting. You would have to do:
s = "A {} string".format('very nice'.replace(' ', '_'))
if you wanted to replace only within the variable (which seems like your objective), or:
s = "A {} string".format('very nice').replace(' ', '_')
if you wanted to replace globally.
Regarding better ways of doing it, I would use os.path.splitext to split the extension, generate the time stamp already in the correct format (if you can control that, of course), instead of using the replace, and do all of that replacing before the os.rename part, so it's more readable (i.e. store the correct strings in variables and use those in the format, instead of doing it all in one line).
Related
I have a string:
s3://tester/test.pdf
I want to exclude s3://tester/ so even if i have s3://tester/folder/anotherone/test.pdf I am getting the entire path after s3://tester/
I have attempted to use the split & partition method but I can't seem to get it.
Currently am trying:
string.partition('/')[3]
But i get an error saying that it out of index.
EDIT: I should have specified that the name of the bucket will not always be the same so I want to make sure that it is only grabbing anything after the 3rd '/'.
You can use str.split():
path = 's3://tester/test.pdf'
print(path.split('/', 3)[-1])
Output:
test.pdf
UPDATE: With regex:
import re
path = 's3://tester/test.pdf'
print(re.split('/',path,3)[-1])
Output:
test.pdf
Have you tried .replace?
You could do:
string = "s3://tester/test.pdf"
string = string.replace("s3://tester/", "")
print(string)
This will replace "s3://tester/" with the empty string ""
Alternatively, you could use .split rather than .partition
You could also try:
string = "s3://tester/test.pdf"
string = "/".join(string.split("/")[3:])
print(string)
To answer "How to get everything after x amount of characters in python"
string[x:]
PLEASE SEE UPDATE
ORIGINAL
Using the builtin re module.
p = re.search(r'(?<=s3:\/\/tester\/).+', s).group()
The pattern uses a lookbehind to skip over the part you wish to ignore and matches any and all characters following it until the entire string is consumed, returning the matched group to the p variable for further processing.
This code will work for any length path following the explicit s3://tester/ schema you provided in your question.
UPDATE
Just saw updates duh.
Got the wrong end of the stick on this one, my bad.
Below re method should work no matter S3 variable, returning all after third / in string.
p = ''.join(re.findall(r'\/[^\/]+', s)[1:])[1:]
I'm using a dict in python in an attempt to replace words in a string with whatever is in the dict. When I try to run my code, however, it prints out the error "ValueError: expected ':' after format specifier." I have no idea where this error might be coming from. Does anyone with more pythonic wisdom have any suggestions for me?
Thanks!
Here's an example of my code:
str = """{fruit}"""
dict = {"fruit":"pears"}
str.replace(**dict)
This should make str contain "pears".
UPDATE
I'm purposefully using the triple quoted strings - in my code I'm trying to do a replace with a multiline string,
Also, my code is already using the .format method. I just decided to mix the words when translating it between my code to here. This is an updated version of my example code that isn't working.
my_dict = """{fruit}"""
dict = {"fruit":"pears"}
string.format(**my_dict)
FINAL UPDATE
Thanks for all of the answers I received below. I didn't do a good job of explaining my problem and decided to simplify it which simplified away my problem. I'm doing some meta programming so I was trying to replace within a C function definition and python was trying to use the "{" as a format identifier. I needed to use "{{" to get python to recognize the bracket as a char and not as some beginning to a format identifier.
You want to use the format() function instead of replace():
string = "{fruit}"
my_dict = {"fruit": "pears"}
print(string.format(**my_dict))
Output
pears
A couple of other things: you shouldn't use the Python keywords str and dict as variable names. I have changed your variable names for clarity.
Also, you were using a multi-line string with triple quotes for no particular reason, so I changed that too.
Another simple method to replace sentence with a dictionary. I hope you like the simplicity. Also look into this Single Pass Multiple Replace
import re
s = "node.js ruby python python3 python3.6"
d = {'python': 'python2.7', 'python3': 'python3.5', 'python3.6': 'python3.7'}
pattern = re.compile(r'\b(' + '|'.join(d.keys()) + r')\b')
result = pattern.sub(lambda x: d[x.group()], s)
print result
This will match whole words only.
I am using a simple '.replace()' function on a string to replace some text with nothing as so:
.replace("('ws-stage-stat', ", '')
I have also tried using a regex to do this, like so:
match3a = re.sub("\(\'ws-stage-stat\', ", "", match3a)
This string is extracted from the source code for the following webpage at line 684:
http://www.whoscored.com/Regions/252/Tournaments/26
I have extracted and cleaned up the rest of the code into some usable data, but this one last bit won't co-operate and stubbornly refuses to be replaced. This seems like a very straight forward problem, but it just won't work for me.
Any ideas?
Thanks
The first replacement should work. Make sure that you're assigning the result of the replacement somewhere, for example:
mystring = mystring.replace("('ws-stage-stat', ", '')
I think you aren't escaping the regex correctly.
This is code my "Patterns" app spit out:
re.sub("\\(\\'ws-stage-stat\\', ", "", match3a)
A quick test showed me that it works correctly.
I'm trying to find a way to print a string in raw form from a variable. For instance, if I add an environment variable to Windows for a path, which might look like 'C:\\Windows\Users\alexb\', I know I can do:
print(r'C:\\Windows\Users\alexb\')
But I cant put an r in front of a variable.... for instance:
test = 'C:\\Windows\Users\alexb\'
print(rtest)
Clearly would just try to print rtest.
I also know there's
test = 'C:\\Windows\Users\alexb\'
print(repr(test))
But this returns 'C:\\Windows\\Users\x07lexb'
as does
test = 'C:\\Windows\Users\alexb\'
print(test.encode('string-escape'))
So I'm wondering if there's any elegant way to make a variable holding that path print RAW, still using test? It would be nice if it was just
print(raw(test))
But its not
I had a similar problem and stumbled upon this question, and know thanks to Nick Olson-Harris' answer that the solution lies with changing the string.
Two ways of solving it:
Get the path you want using native python functions, e.g.:
test = os.getcwd() # In case the path in question is your current directory
print(repr(test))
This makes it platform independent and it now works with .encode. If this is an option for you, it's the more elegant solution.
If your string is not a path, define it in a way compatible with python strings, in this case by escaping your backslashes:
test = 'C:\\Windows\\Users\\alexb\\'
print(repr(test))
In general, to make a raw string out of a string variable, I use this:
string = "C:\\Windows\Users\alexb"
raw_string = r"{}".format(string)
output:
'C:\\\\Windows\\Users\\alexb'
You can't turn an existing string "raw". The r prefix on literals is understood by the parser; it tells it to ignore escape sequences in the string. However, once a string literal has been parsed, there's no difference between a raw string and a "regular" one. If you have a string that contains a newline, for instance, there's no way to tell at runtime whether that newline came from the escape sequence \n, from a literal newline in a triple-quoted string (perhaps even a raw one!), from calling chr(10), by reading it from a file, or whatever else you might be able to come up with. The actual string object constructed from any of those methods looks the same.
I know i'm too late for the answer but for people reading this I found a much easier way for doing it
myVariable = 'This string is supposed to be raw \'
print(r'%s' %myVariable)
try this. Based on what type of output you want. sometime you may not need single quote around printed string.
test = "qweqwe\n1212as\t121\\2asas"
print(repr(test)) # output: 'qweqwe\n1212as\t121\\2asas'
print( repr(test).strip("'")) # output: qweqwe\n1212as\t121\\2asas
Get rid of the escape characters before storing or manipulating the raw string:
You could change any backslashes of the path '\' to forward slashes '/' before storing them in a variable. The forward slashes don't need to be escaped:
>>> mypath = os.getcwd().replace('\\','/')
>>> os.path.exists(mypath)
True
>>>
Just simply use r'string'. Hope this will help you as I see you haven't got your expected answer yet:
test = 'C:\\Windows\Users\alexb\'
rawtest = r'%s' %test
I have my variable assigned to big complex pattern string for using with re module and it is concatenated with few other strings and in the end I want to print it then copy and check on regex101.com.
But when I print it in the interactive mode I get double slash - '\\w'
as #Jimmynoarms said:
The Solution for python 3x:
print(r'%s' % your_variable_pattern_str)
Your particular string won't work as typed because of the escape characters at the end \", won't allow it to close on the quotation.
Maybe I'm just wrong on that one because I'm still very new to python so if so please correct me but, changing it slightly to adjust for that, the repr() function will do the job of reproducing any string stored in a variable as a raw string.
You can do it two ways:
>>>print("C:\\Windows\Users\alexb\\")
C:\Windows\Users\alexb\
>>>print(r"C:\\Windows\Users\alexb\\")
C:\\Windows\Users\alexb\\
Store it in a variable:
test = "C:\\Windows\Users\alexb\\"
Use repr():
>>>print(repr(test))
'C:\\Windows\Users\alexb\\'
or string replacement with %r
print("%r" %test)
'C:\\Windows\Users\alexb\\'
The string will be reproduced with single quotes though so you would need to strip those off afterwards.
To turn a variable to raw str, just use
rf"{var}"
r is raw and f is f-str; put them together and boom it works.
Replace back-slash with forward-slash using one of the below:
re.sub(r"\", "/", x)
re.sub(r"\", "/", x)
This does the trick
>>> repr(string)[1:-1]
Here is the proof
>>> repr("\n")[1:-1] == r"\n"
True
And it can be easily extrapolated into a function if need be
>>> raw = lambda string: repr(string)[1:-1]
>>> raw("\n")
'\\n'
i wrote a small function.. but works for me
def conv(strng):
k=strng
k=k.replace('\a','\\a')
k=k.replace('\b','\\b')
k=k.replace('\f','\\f')
k=k.replace('\n','\\n')
k=k.replace('\r','\\r')
k=k.replace('\t','\\t')
k=k.replace('\v','\\v')
return k
Here is a straightforward solution.
address = 'C:\Windows\Users\local'
directory ="r'"+ address +"'"
print(directory)
"r'C:\\Windows\\Users\\local'"
I've written an XML parser in Python and have just added functionality to read a further script from a different directory.
I've got two args, first is the path where I'm parsing XML. Second is a string in another XML file which I want to match with the first path;
arg1 = \work\parser\main\tools\app\shared\xml\calculators\2012\example\calculator
path = calculators/2012/example/calculator
How can I compare the two strings to match identify that they're both referencing the same thing and also, how can I strip calculator from either string so I can store that & use it?
edit
Just had a thought. I have used a Regex to get the year out of the path already with year = re.findall(r"\.(\d{4})\.", path) following a problem Python has with numbers when converting the path to an import statement.
I could obviously split the strings and use a regex to match the path as a pattern in arg1 but this seems a long way round. Surely there's a better method?
Here I am assuming you are actually talking about strings, and not file paths - for which #mgilson's suggestion is better
How can I compare the two strings to match identify that they're both
referencing the same thing
Well first you need to identify what you mean by "the same thing"
At first glance it seems that if the the second string ends with the first string with the reversed slash, you have a match.
arg1 = r'\work\parser\main\tools\app\shared\xml\calculators\2012\example\calculator'
arg2 = r'calculators/2012/example/calculator'
>>> arg1.endswith(arg2.replace('/','\\'))
True
and also, how can I strip calculator from
either string so I can store that & use it?
You also need to decide if you want to strip the first calculator, the last calculator or any occurance of calculator in the string.
If you just want to remove the last string after the separator, then its simply:
>>> arg2.split('/')[-1]
'calculator'
Now to get the orignal string back, without the last bit:
>>> '/'.join(arg2.split('/')[:-1])
'calculators/2012/example'
check out os.path.samefile:
http://docs.python.org/library/os.path.html#os.path.samefile
and os.path.dirname:
http://docs.python.org/library/os.path.html#os.path.dirname
or maybe os.path.basename (I'm not sure what part of the string you want to keep).
Here, try this:
arg1 = "\work\parser\main\tools\app\shared\xml\calculators\2012\example\calculator"
path = "calculators/2012/example/calculator"
arg1=arg1.replace("/","\\")
path=path.replace("/","\\")
if str(arg1).endswith(str(path)) or str(path).endswith(str(arg1)):
print "Match"
That should work for your needs. Cheers :)