Это одна из первых вещей, которая возникает при поиске этой информации, поэтому я хотел бы добавить то, что я нашел для версии 3.2 программы pyinstaller. Если вы уже упаковали свой скрипт, запустив
pyinstaller --onefile your_script.py
или аналогичный, вы можете отредактировать файл your_script.spec, чтобы избавиться от консоли.
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='main',
debug=False,
strip=False,
upx=True,
console=True )
Просто измените значение консоли на False. Затем выполните:
pyinstaller your_script.spec
Кроме того, если вы вносите изменения в свой код, выполните приведенную выше команду, чтобы они были отражены в your_script.exe. Я нашел это полезным для отладки различных других проблем.
If you want to do the replacement in place, you need to use:
str.gsub!(/\s/,'')
Alternatively, gsub returns the string with the replacements
str2 = str.gsub(/\s/,'')
EDIT: Based on your answer, it looks like you have some unprintable characters embedded in the string, not spaces. Using /\D/ as the search string may be what you want. The following will match any non-digit character and replace it with the empty string.
str.gsub!(/\D/,'')
print "5 900 000".gsub(/\s/, '')
Works for me.
Are you affecting the result to the variable ?
>> "5 900 00".gsub(' ','')
=> "590000"
Is it really a string?
.gsub returns the value, if you want to change the variable try .gsub!(" ", "")
"5 900 000".gsub(/\s/,'')
works fine
From what I see you wrote gsub dot (foo,bar) where it must be string.gsub(foo,bar)
Just for kicks: do you even need a regular expression here? String#tr
should do the trick just fine:
telemachus ~ $ irb
>> "500 500 12".tr(' ', '')
=> "50050012"
>> "500 500 12".tr!(' ', '')
=> "50050012"
As with gsub
and gsub!
, the !
method makes the change in place as opposed to returning the changed result. I don't know which you want here.
In a case like this, tr
seems more straightforward to me. I'm not looking for optimization, but it is good to remember that there are lots of string methods other than regular expressions.
Я предлагаю сделать str.gsub! (/ \ S + /, '')
из соображений эффективности.
do you mean
str.gsub!.(/\s/,'')
with the exclamation mark?
Самое смешное, что когда я печатаю строку, я получаю
697 \ 302 \ 240000
, но в базу данных попадает: 697 000. Я знаю, что шаблоны, которые я привел, должны работать так же хорошо, как и ваши предложения, но это кажется немного «хитрым» случаем: -)