is there a reason why the len function displays the wrong lengh of a word (no spaces etc.)? - import

following scenario: I want to display the length of a random word selected from a simple .txt list like this:
composer
circulation
fashionable
prejudice
progress
salesperson
disappoint
I used the following code to display one of these words from the list:
random_word_generator = open("random_words.txt", "r")
random_words = list(random_word_generator)
secret_word = random.choice(random_words)
however, whenever I want to print the length of the word by using:
print("My secret word is " + str(len(secret_word)))
It shows the length of the word - 1 character
like:
progress --> should be 8 letters, but python displays 7...
Do you know how this issue could be solved?
Btw: there are no spaces whatsoever in my .txt file
Kind Regards and many thanks in advance

You should mention the language you're using in the title and the tags.
To your question: Can you please paste the full code you're using? I tried your example, and it displays the correct length of each word (+ 1 for the newline character, which you could remove by calling .strip()), so I'm guessing you do something different.
random_word_generator = open("random_words.txt", "r")
random_words = list(random_word_generator) # ['composer\n', 'circulation\n', 'fashionable\n', 'prejudice\n', 'progress\n', 'salesperson\n', 'disappoint\n']
secret_word = random.choice(random_words) # "progress\n"
print("My secret word is " + str(len(secret_word))) # "My secret word is 9"
print("My secret word w/o newline is " + str(len(secret_word.strip()))) # "My secret word w/o newline is 8"

Related

Special character in key in the conf file for Internationlization in play framework

I am trying to use the Internationalization feature of the Play Framework.
It involves the creation of a new conf file for each language that we want to support. Example for french we create a messages.fr file in the conf folder.
Inside it we define key-values like this:
Hello.World = 'Bonjour le monde'
Now the issue is that I have lines that contain characters like "," and "(" and if these are included in the key then we get the error in parsing from the MessageApi
Example
Hello.(World) = 'Bonjour (le monde)'
Here the "(" before and after World is throwing an error while parsing.
Anyone having any idea how we could achieve this?
Try to escape these special characters:
Hello.\(World\) = 'Bonjour (le monde)'
Other examples:
string_one = String for translation 1
string_two = one + one \= two
# String key with spaces
key\ with\ spaces = This is the value that could be looked up with the key "key with spaces".
# Backslash in value should be escaped by another backslash
path=c:\\wiki\\ templates
Also, you can try to escape special characters by using Java Unicode:
Hello.\u0028World\u0029 = 'Bonjour (le monde)'
Reference - How to escape the equals sign in properties files

How can I get the substrings with spaces as end delimiter

String text ="65:234567
69:456789678
74:45678";
How do I get the text following 65: .I should be able to retrieve 234567.
I would not be able to get the text using str.substring(firstIdx + secondIdx) as the no of digits post ":" might change.
Please advice .

Searching for a literal hyphen in powershell

I am using powershell to split a multipage word document into single pages and then deleting all the pages I don't need. The problem is I am copying the text and searching for a pattern and the hyphen is throwing me off. This is the relevant code. If I remove the hyphen and just search for "Order Number", it works but it comes back with two pages instead of just the one I want with the pattern "Order Number -". I have tried a lot of configurations, escaping the hypen `-, --%, etc but nothing works, everything I run that includes the hyphen just outputs 8 DeleteMe pages, instead of 1 Docuemnt and 7 DeleteMe
$Pattern = "Order Number -"
$rngPg.End = $word.Selection.Start
$rngPg.Copy()
#Get Name
$regex = [Regex]::Match($rngPg.Text, $Pattern)
if($regex.Success)
{
$id = "Document" + $i
}
else
{
$id = "DeleteMe_" + $i
}
Change your pattern to:
$Pattern = 'Order Number [\u2010-\u2015-]'
Explanation:
Word has an AutoFormat feature that loves to automagically turn hyphens into dashes - that may be the case here.
English language Office turns hyphens into unicode characters with the codepoint value 0x2013, but it may vary depending on locale and installed language packs, thus the character set from 0x2010 to 0x2015 + -

PHP - How to identify e-mail addresses from input containing lines of misc data

Apologizing in advance for yet another email pattern matching query.
Here is what I have so far:
$text = strtolower($intext);
$lines = preg_split("/[\s]*[\n][\s]*/", $text);
$pattern = '/[A-Za-z0-9_-]+#[A-Za-z0-9_-]+\.([A-Za-z0-9_-][A-Za-z0-9_]+)/';
$pattern1= '/^[^#]+#[a-zA-Z0-9._-]+\.[a-zA-Z]+$/';
foreach ($lines as $email) {
preg_match($pattern,$email,$goodies);
$goodies[0]=filter_var($goodies[0], FILTER_SANITIZE_EMAIL);
if(filter_var($goodies[0], FILTER_VALIDATE_EMAIL)){
array_push($good,$goodies[0]);
}
}
$Pattern works fine but .rr.com addresses (and more issues I am sure) are stripped of .com
$pattern1 only grabs emails that are on a line by themselves.
I am pasting in a whole page of miscellaneous text into a textarea that contains some emails from an old data file I am trying to recover.
Everything works great except for the emails with more than one "." either before or after the "#".
I am sure there must be more issues as well.
I have tried several patterns I have found as well as some i tried to write.
Can someone show me the light here before I pull my remaining hair out?
How about this?
/((?:\w+[.]*)*(?:\+[^# \t]*)?#(?:\w+[.])+\w+)/
Explanation: (?:\w+[.])* recognizes 0 or more instances of strings of word characters (alphanumeric + _) optionally separated by strings of periods. Next, (?:\+[^# \t]*)? recognizes a plus sign followed by zero or more non-whitespace, non-at-sign characters. Then we have the # sign, and finally (?:\w+[.])+\w+, which matches a sequence of word character strings separated by periods and ending in a word character string. (ie, [subdomain.]domain.topleveldomain)

Vim: change formatting of variables in a script

I am using vim to edit a shell script (did not use the right coding standard). I need to change all of my variables from camel-hum-notation startTime to caps-and-underscore-notation START_TIME.
I do not want to change the way method names are represented.
I was thinking one way to do this would be to write a function and map it to a key. The function could do something like generating this on the command line:
s/<word under cursor>/<leave cursor here to type what to replace with>
I think that this function could be applyable to other situations which would be handy. Two questions:
Question 1: How would I go about creating that function.
I have created functions in vim before the biggest thing I am clueless about is how to capture movement. Ie if you press dw in vim it will delete the rest of a word. How do you capture that?
Also can you leave an uncompleted command on the vim command line?
Question 2: Got a better solution for me? How would you approach this task?
Use a plugin
Check the COERCION section at the bottom of the page:
http://www.vim.org/scripts/script.php?script_id=1545
Get the :s command to the command line
:nnoremap \c :%s/<C-r><C-w>/
<C-r><C-w> gets the word under the cursor to command-line
Change the word under the cursor with :s
:nnoremap \c lb:s/\%#<C-r><C-w>/\=toupper(substitute(submatch(0), '\<\#!\u', '_&', 'g'))/<Cr>
lb move right, then to beginning of the word. We need to do this to get
the cursor before the word we wish to change because we want to change only
the word under the cursor and the regex is anchored to the current cursor
position. The moving around needs to be done because b at the
start of a word moves to the start of the previous word.
\%# match the current cursor position
\= When the substitute string starts with "\=" the remainder is interpreted as an expression. :h sub-replace-\=
submatch(0) Whole match for the :s command we are dealing with
\< word boundary
\#! do not match the previous atom (this is to not match at the start of a
word. Without this, FooBar would be changed to _FOO_BAR)
& in replace expressions, this means the whole match
Change the word under the cursor, all matches in the file
:nnoremap \a :%s/<C-r><C-w>/\=toupper(substitute(submatch(0), '\<\#!\u', '_&', 'g'))/g<Cr>
See 3. for explanation.
Change the word under the cursor with normal mode commands
/\u<Cr> find next uppercase character
i_ insert an underscore.
nn Search the last searched string twice (two times because after exiting insert mode, you move back one character).
. Repeat the last change, in this case inserting the underscore.
Repeat nn. until all camelcases have an underscore added before them, that is, FooBarBaz has become Foo_Bar_Baz
gUiw uppercase current inner word
http://vim.wikia.com/wiki/Converting_variables_to_camelCase
I am not sure what you understand under 'capturing movements'. That
said, for a starter, I'd use something like this for the function:
fu! ChangeWord()
let l:the_word = expand('<cword>')
" Modify according to your rules
let l:new_var_name = toupper(l:the_word)
normal b
let l:col_b = col(".")
normal e
let l:col_e = col(".")
let l:line = getline(".")
let l:line = substitute(
\ l:line,
\ '^\(' . repeat('.', l:col_b-1) . '\)' . repeat('.', l:col_e - l:col_b+1),
\ '\1' . l:new_var_name,
\ '')
call setline(".", l:line)
endfu
As to leaving an uncompleted command on the vim command line, I think you're after
:map ,x :call ChangeWord(
which then can be invoked in normal mode by pressing ,x.
Update
After thinking about it, this following function is a bit shorter:
fu! ChangeWordUnderCursor()
let l:the_word = expand('<cword>')
"" Modify according to your rules
let l:new_var_name = '!' . toupper(l:the_word) . '!'
normal b
let l:col_b = col(".")
normal e
let l:col_e = col(".")
let l:line = getline(".")
exe 's/\%' . l:col_b . 'c.*\%' . (l:col_e+1) .'c/' . l:new_var_name . '/'
endfu