So I have been browsing through online sites to read a file line by line and I come to this part of this code:
print("Line {}: {}".format(linecount, line))
I am quite confused as to what is happening here. I know that it is printing something, but it shows:
"Line{}"
I do not understand what this means. I know that you could write this:
foo = "hi"
print(f"{foo} bob")
But I don't get why there are empty brackets.
Empty braces are equivalent to numeric braces numbered from 0:
>>> '{}: {}'.format(1,2)
'1: 2'
>>> '{0}: {1}'.format(1,2)
'1: 2'
Just a shortcut.
But if you use numerals you can control the order:
>>> '{1}: {0}'.format(1,2)
'2: 1'
Or the number of times something is used:
>>> '{0}: {0}, {1}: {1}'.format(1,2)
'1: 1, 2: 2'
Which you cannot do with empty braces.
Doing "My {} string is {}".format('formatted', 'awesome') just fills in the curly braces with the args you provide in the format function in the order you enter the arguments.
So the first {} in the above string would get 'formatted' and the second in that case would get 'awesome'.
It's an older version of formatting strings than f strings (which I'm glad I started learning Python when these already came out), but you can equally write something like this similar to f-strings:
>>> template = 'I love {item}. It makes me {emotion}'
>>>
>>> my_sentence = template.format(item='fire', emotion='calm')
>>> print(my_sentence)
I love fire. It makes me calm.
This is a different way to interpolate strings in Python.
Docs: https://docs.python.org/3/tutorial/inputoutput.html#fancier-output-formatting
The usage of string interpolations like this f'Results of the {year} {event}' came in Python 3.6.
Related
How to remove NUL characters / white space in string?
I've tried to use the entire answer and comments here. Seems none of them work for me.
For example the string is m o d e m
for part_entry in part_list:
partStr = unicode(part_entry[5])
partStr.rstrip(' \t\r\n\0')
logging.debug('******')
logging.debug('PartName= '+partStr)
This is what currently I compile.
I tried to log it, and it's rendering output like this;
Try this:
content = []
with open('file.ext', as 'r') as file_in:
content = file_in.readlines()
new_content = []
for line in file:
clean_line = line.replace('\00', '')
new_content.append(clean_line)
with open('outfile.ext', 'w') as file_out:
file_out.writelines(new_content)
As #Matt_G mentioned, you can replace characters in a string with str.replace(old, new). This method is available in python 2.7 and 3.x.
To cite the documentation for str.replace(old, new, [count]): 'Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.' This means you can use str.replace for replacing the substring \x00, aka NUL, in your string with this method.
Since you wanted a solution for a returned value, see below:
def work():
return "Foo \0"
result = work()
print(result) # -> 'Foo NUL'
print(result.replace('\0', 'Bar')) # -> 'Foo Bar'
Another example with the code from your answer (Example is for python 2, since unicode method was used):
for part_entry in part_list:
partStr = unicode(part_entry[5]).replace('\0', '')
partStr.rstrip(' \t\r\n\0')
logging.debug('******')
logging.debug('PartName= ' + partStr)
To directly remove NUL, alias \0 or \00, line-by-line, please see the answer from Matt_G.
If you want to remove multiple characters at once (guessing based on the usage of str.rstrip), please use str.translate (docs: 2.7 | 3.9) instead (also for python 2 | See this SO post for the usage of translate for unicode strings):
for part_entry in part_list:
partStr = unicode(part_entry[5]).translate({
ord('\t'): None,
ord('\r'): None,
ord('\n'): None,
ord('\0'): None
})
logging.debug('******')
logging.debug('PartName= ' + partStr)
I hope this helped you and if they're any open questions feel free to ask right away.
PS: Learn more about the NUL character
I want to find the first float that appears in a string using Python 3.
I looked at other similar questions but I couldn't understand them and when I tried to implement them they didn't work for my case.
An example string would be
I would like 1.5 cookies please
I'm pretty sure there's more elegant solution, but this one works for your specific case:
s = 'I would like 1.5 cookies please'
for i in s.split():
try:
#trying to convert i to float
result = float(i)
#break the loop if i is the first string that's successfully converted
break
except:
continue
print(result) #1.5
You can find this using regex, notice this pattern will only return the substring if it's already in float type, i.e. decimal fomatting, so something like this:
>>> import re
>>> matches = re.findall("[+-]?\d+\.\d+", "I would like 1.5 cookies please")
As you say you want only the first one:
>>> matches[0]
'1.5'
Edit: Added [+-]? to the pattern for it to recognize negative floats, as pistache recommended!
If you expect whitespace separated decimal floats, using str methods and removing -+.:
s = 'I would like 1.5 cookies please'
results = [t for t in s.split()
if t.lstrip('+-').replace('.', '', 1).isdigit()]
print(results[0]) #1.5
lstrip is used to remove the sign only on the lefthand side of the text, and the third argument to replace is used to replace only one dot in the text. The exact implementation depends on the how you expect floats to be formatted (support whitespace between sign, etc).
I would use a regex. below also checks for negative values.
import re
stringToSearch = 'I would like 1.5 cookies please'
searchPattern = re.compile(".*(-?[0-9]\.[0-9]).*")
searchMatch = searchPattern.search(stringToSearch)
if searchMatch:
floatValue = searchMatch.group(1)
else:
raise Exception('float not found')
You can use PyRegex to check the regex.
I want to find out if a string contains the word "randomize". This word my exist in and outside of brackets in the string but I am only interested if the word exists IN SIDE of the brackets.
mystring = "You said {single order='randomize'} that P.E is...Why?"
I understand that i have to use regex for this but my attampts have failed thus far.
Essentially I want to say:
look for "randomize" and check if its in brackets.
Thanks
You could use some negated classes:
>>> import re
>>> mystring = "You said {single order='randomize'} that P.E is...Why?"
>>> if mystring.find("randomize") != -1:
... if re.search(r'{[^{}]*randomize[^{}]*}', mystring):
... print("'randomize' present within braces")
... else:
... print("'randomize' present but not within braces")
... else:
... print("'randomize' absent")
# => 'randomize' present within braces
This is the kind of thing that's very difficult for regex to do. You see if you do something like re.escape(r"{.*?randomize.*?}"), you can match something like "Hello there, I'm going to {break} your randomize regex {foobar}" and it will return "{break} your randomize regex {foobar}". You can probably pull this off with lookahead and lookbehind assertions, but not without telling us if the brackets can be nested, since this will then fail on "I'm going to break you {now with randomize {nested} brackets}"
As per your update that the brackets will never be nested, this regex should match:
re.search("{[^}]*?randomize.*?}", mystring)
And you can access the group using .group(0). Put it all together to do something like:
for mystring in group_of_strings_to_test:
if re.search("{[^}]*?randomize.*?}", mystring).group(0):
# it has "randomize" in a bracket
else:
# it doesn't.
To assure you're not inside nested {}'s it could be
{[^{}]*randomize[^{}]*}
The naive simple method:
>>> import re
>>> mystring = "You said {single order='randomize'} that P.E is...Why?"
>>> print re.search('{.*randomize.*}', mystring).group(0)
Once we have this, we can improve it bit by bit. For instance, this is called a greedy regex, which means:
>>> print re.search('{.*randomize*}', "{FOO {randomize} BAR}").group(0)
{FOO {randomize} BAR}
You'll probably want it to be non-greedy, so you should use '.*?' instead:
>>> print re.search('{.*?randomize.*?}', mystring).group(0)
Also, it will not handle nested:
>>> print re.search('{.*?randomize.*?}', "{FOO} randomize {BAR}").group(0)
{FOO} randomize {BAR}
If you want to handle simple nested, you may want to match all characters except other brackets.
>>> print re.search('{[^}]*randomize[^{]*}', mystring).group(0)
s = "A Colon is beside me"
print s,":"
I should get
A Colon is beside me :
But I wanna get
>>>A Colon is beside me:
How?
Concatenate the strings:
print s + ':'
or use string formatting:
print '{0}:'.format(s)
On python 3, the print() function can also be told to use an empty separator between multiple arguments, instead of a space:
print(s, ':', sep='')
This definatly works. (I am new to the site and I have put normal text not code!?)
print "%s:" % s
The %s means insert a variable here and the second s means insert the specific variable 's
This is for summarization purpose.
There are four ways to print statement:
s = "A Colon is beside me"
Data Type Parsing Mode:
print("%s:" %s)
Variables Concatenation Mode:
print(s+":")
format() method:
print("{}:".format(s))
Using Built-in function:
print(s,':',sep='')
I'm looking for a regular expression, implemented in Python, that will match on this text
WHERE PolicyGUID = '531B2310-403A-13DA-5964-E2EFA56B0753'
but will not match on this text
WHERE AsPolicy.PolicyGUID = '531B2310-403A-13DA-5964-E2EFA56B0753'
I'm doing this to find places in a large piece of SQL where the developer did not explicitly reference the table name. All I want to do is print the offending lines (the first WHERE clause above). I have all of the code done except for the regex.
re.compile('''WHERE [^.]+ =''')
Here, the [] indicates "match a set of characters," the ^ means "not" and the dot is a literal period. The + means "one or more."
Was that what you were looking for?
something like
WHERE .*\..* = .*
not sure how accurate can be, it depends on how your data looks... If you provide a bigger sample it can be refined
Something like this would work in java, c#, javascript, I suppose you can adapt it to python:
/WHERE +[^\.]+ *\=/
>>> l
["WHERE PolicyGUID = '531B2310-403A-13DA-5964-E2EFA56B0753' ", "WHERE AsPolicy.P
olicyGUID = '531B2310-403A-13DA-5964-E2EFA56B0753' "]
>>> [line for line in l if re.match('WHERE [^.]+ =', line)]
["WHERE PolicyGUID = '531B2310-403A-13DA-5964-E2EFA56B0753' "]