Я пытался научиться кодировать с книгой Аль Суигерта. Прямо сейчас я застрял в том, чтобы заставить программу получать текст и / или аргумент из одного ввода. Я знаю, что проще использовать два отдельных входа, но я хочу сделать это, используя только один.
Я также не знаю, в чем разница между r 'и rf' в Regex.
#python 3
#A program that does exactly the same thing as the split function
import re
def striperoo(text, argument):
if argument != '':
argRegex = re.compile(rf'{argument}')
while True:
argCheck = argRegex.search(text)
if argCheck != None:
startOfArg = argCheck.span()[0]
endOfArg = argCheck.span()[1]
text = text[:startOfArg] + text[endOfArg:]
else:
print(text)
break
else:
spcRegexBegin = re.compile(r'^\s+')
spcRegexEnd = re.compile(r'\s+$')
while True:
spcAtBeginning = spcRegexBegin.search(text)
spcAtEnd = spcRegexEnd.search(text)
if spcAtBeginning != None:
blankSpacesSpan = spcAtBeginning.span()[1]
text = text[blankSpacesSpan:]
elif spcAtEnd != None:
spacesAtEnd = spcAtEnd.span()[0]
text = text[:spacesAtEnd]
else:
print(text)
break
texto, argumento = input('Please type text and argument using the format text, argument: ').split(', ')
striperoo(texto, argumento)
Может ли кто-нибудь помочь мне? Как и ожидалось, когда я набираю только одну переменную, появляется следующая ошибка:
Traceback (most recent call last):
File "C:\strip.py", line 33, in <module>
texto, argumento = input('Please type text and argument using the format text, argument: ').split(', ')
ValueError: not enough values to unpack (expected 2, got 1)
Она должна преобразовывать строки вроде ' Hola mundo cruel '
в 'Hola mundo cruel'
и 'AholaAmundoAcruel, A'
в 'holamundocruel'
.
Спасибо
Хайме