Полоса/замена располагает с интервалами в строке

Это одна из первых вещей, которая возникает при поиске этой информации, поэтому я хотел бы добавить то, что я нашел для версии 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. Я нашел это полезным для отладки различных других проблем.

11
задан Luís Ramalho 5 July 2013 в 17:52
поделиться

8 ответов

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/,'')
30
ответ дан 3 December 2019 в 00:58
поделиться
print "5 900 000".gsub(/\s/, '')

Works for me.

Are you affecting the result to the variable ?

0
ответ дан 3 December 2019 в 00:58
поделиться
>> "5 900 00".gsub(' ','')
=> "590000"

Is it really a string?

.gsub returns the value, if you want to change the variable try .gsub!(" ", "")

11
ответ дан 3 December 2019 в 00:58
поделиться

"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)

1
ответ дан 3 December 2019 в 00:58
поделиться

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.

5
ответ дан 3 December 2019 в 00:58
поделиться

Я предлагаю сделать str.gsub! (/ \ S + /, '') из соображений эффективности.

2
ответ дан 3 December 2019 в 00:58
поделиться

do you mean

str.gsub!.(/\s/,'')

with the exclamation mark?

0
ответ дан 3 December 2019 в 00:58
поделиться

Самое смешное, что когда я печатаю строку, я получаю

697 \ 302 \ 240000

, но в базу данных попадает: 697 000. Я знаю, что шаблоны, которые я привел, должны работать так же хорошо, как и ваши предложения, но это кажется немного «хитрым» случаем: -)

0
ответ дан 3 December 2019 в 00:58
поделиться
Другие вопросы по тегам:

Похожие вопросы: