My keywords
keywords = ['monday', 'tuesday', 'wednesday', 'thursday']
My txt file content:
Today is tuesday and tomorrow is wednesday
Expected Output should be:
tuesday
wednesday
You can use regex to check whether the keywords are in the in text or not.
import re
keywords=['monday','tuesday','wednesday','thursday','friday']
with open('text.txt') as f:
txt=f.read()
for i in keywords:
if re.search(r'\b{}\b'.format(i),txt):
print i
The resulting output would be :
tuesday
wednesday
Try following. It will open the file and read it line by line. Each key word will be checked in against line whether it exists or not. You can also use intersection of the sets.
for line in open('file.txt'):
for k in keywords:
if k in line:
print(k)
Related
When trying to get an output of "#.## M" I receive an error, but when I remove the M, I do not. Having the M at the end is imperative to the end result. Formatting alone outside of the formula does not work.
I've tried several web searches that did not fix the issue. I don't know what else to attempt.
=(TEXT('National Economy & Finance'!C13, "#.## M"))
I expect an output of 42.42 M but instead receive the following error:
Invalid format pattern '#.## M' in TEXT evaluation.
You just need to escape the M:
=(TEXT('National Economy & Finance'!C13, "#.## \M"))
"#.##" would be interpreted as a decimal number and "M" would normally be interpreted as month, so you can't have both together in the same format.
You can also just put
=(TEXT('National Economy & Finance'!C13, "#.##"))&" M"
I couldn't really clarify what I'm asking in the title. I an integer for a day and a month. I have to print the month with a 0 in front of it if it's one digit only.
For example 04 if month = 4 and so on.
This is how it's supposed to look like in C#:
Console.WriteLine("{0}.{1:00}", day, month);
Thank you.
int month = 4;
DecimalFormat formater = new DecimalFormat("00");
String month_formated = formater.format(month);
Besides the answer Fernando Lahoz provided (which is pretty specific to your case: decimal formating) you can also use System.out.format in Java which allows you to specify a format-string while printing to System.out (the format function is applicable to any PrintStream though). In your case
System.out.format("%2d %2d", day, month)
should do the trick. The %dis used for decimal integers and you can then specify any width you want just before the 'd' (2 in your case).
If you want to access the string formed for later use and not (only) print it you can use String.format. It uses the same format as System.out.format but returns the String that is formed.
A complete syntax for all formats(string, decimal, floating point, calendar, date/time, ...) can be found here.
If you'd like a quick tuto on number-formatting you can check this link or this link instead.
Good luck!
I would like to check whether a year was found within a string. Something like
if string.scan(/\d{4}/).first == TRUE
for example a string looks like "there were 3 earthquakes in 2007"
Any suggestions?
If you want to match standalone 4 digit string, you may consider a regex with word boundaries:
!('It is 2016 now.' =~ /\b\d{4}\b/).nil? # => true
or - a more real world sample usage:
if string =~ /\b\d{4}\b/
The \b\d{4}\b matches any 4 digits that are not preceded nor followed with word characters (digits, letters or underscore), so there will be no match in 02312345.
Also, in case you want to precise to current century, or the 20th century, you may use /\b(?:19|20)\d{2}\b/ regex.
To extract the digits, use s[/\b\d{4}\b/].
'It was in 2015/16.'[/\b\d{4}\b/] # => 2015
See the Ruby demo
I'm trying to return a date as a string but remove the first 0 from the hour returned.
This is the code I'm using:
t = Time.new
current_date = Time.local(t.year, t.month, t.day, t.hour, t.min/30*30)
time_str = current_date.strftime("%I.%M+%p").downcase
puts time_str
Outputs:
03.00+pm
Expected output:
3.00+pm # need to remove the last zero from the hour(s)
just use %l instead of %I
.strftime("%l.%M+%p")
Just add one point: if you want to quickly find the format string, man date will give you the correct answer if you are on OS X or Linux.
If the strftime and date format string differs, you can always find it up in the Ruby document, here's the document for strftime.
I would like to return the local time as string but with leading zeros. I tried this:
{{Year, Month, Day}, {Hour, Minute, Second}} = erlang:localtime().
DateAsString = io_lib:format("~2.10.0B~2.10.0B~4.10.0B~2.10.0B~2.10.0B~2.10.0B",
[Month, Day, Year, Hour, Minute, Second]).
But if some of the components is one digit, the returned string is:
[["0",57],"29","2011","17","33","34"]
The current month 9 is printed as ["0",57].
Please, help.
Thank you.
Try:
1> lists:flatten([["0",57],"29","2011","17","33","34"]).
"09292011173334"
io_lib:format/2 (and it's companion io:format/2) actually returns a deep IO list. Such a list is printable and can be sent on a socket or written to a file just as a flat string, but is more efficient to produce. Flattening is often useless, because in all cases where the string will be printed or output to a file/socket it will automatically be flattened by Erlang.
You want to be using something like this:
DateAsString = io_lib:format("~2..0w~2..0w~4..0w~2..0w~2..0w~2..0w",
[Month, Day, Year, Hour, Minute, Second]).
The more common w format modifier does the right thing here, what with base and such, so there's no need to use the more complex B modifier. 2..0 says "2 characters wide, zero padded, no precision specified." We don't need precision here, since we're dealing with integers.