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.
Related
I was padding an output in a print statement with a simple code which uses .format() function
print('{:-<100}'.format('xyz'))
I was able to get a similar output using f-strings by creating a new variable
library = 'xyz'
print(f'{library:-<100}')
Question: Is there a way to add the string 'xyz' inside the f-string without having to create a new variable?
I tried the code below, but it gave me an error:
print(f'xyz:-<100')
If I understand your question right, then:
You can just use double-qouted string inside single-quoted f-string
print(f'{"xyz":-<100}')
and optional without f-string and format
print("xyz".ljust(100, "-"))
If I'm not mistaken, what you want to do is:
print(f"{'xyz':-<100}") # You can use expressions here, not only variables!
PS: Regarding the error, are you sure you are running Python +3.6?
Yes, there is a way to add the string ('xyz') inside the fstring without having to create a new variable.
Just add the string 'xyz' outside of the curly brackets '{}'
Example:
print(f'xyz{:-<100}')
I have the following string
'file path = data/imagery/256:0:10.0:34:26:-1478/256:0:10.0:34:26:-1478_B02_10m.tif'
I am trying to get 256:0:10.0:34:26:-1478_B02_10m.tif from the string above
but if I run
os.path.splitext(filepath.strip('data/imagery/256:0:10.0:34:26:-1478'))[0]
It outputs '_B02_10m'
Same with filepath.rstrip('data/imagery/256:0:10.0:34:26:-1478')
Assuming you want all the string data after the / you can always use string.split. This spits your string into a list of strings split on the split string. Then you would only need the final item of this list.
string_var.split("/")[:-1]
See more official python docs on string.split here.
Python's strip doesn't strip the string in the argument but uses it as a list of characters to remove from the original string see: https://docs.python.org/3/library/stdtypes.html#str.strip
EDIT: This doesn't provide a meaningful solution, see accepted answer.
Instead of using strip you should use string.split()
Following piece of code gets you the required substring:
filepath = "data/imagery/256:0:10.0:34:26:-1478/256:0:10.0:34:26:-1478_B02_10m.tif"
print(filepath.split('/')[-1])
Output:
256:0:10.0:34:26:-1478_B02_10m.tif
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:]
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).
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'"