如何在python中连接整数和字符串

时间:2019-04-12 13:14:19

标签: python python-3.x

我正在尝试使用re库搜索具有指定模式的子字符串。我编写了一个函数来执行此操作,但最终收到此错误:类型错误:无法连接str和整数。下面是我的功能;

def searchValue(obs, concept):
try:
    found = re.search('## !!'concept+'=(.+?)!! ##',obs)
except AttributeError:
    found = 'null'
return found

obs= '!!1834=7850!! ## !!1915=Patient is awaiting imaging results, then start darcabazine 250 ml every 21 days.!! ## !!1915=Patient is HIV positive since 2016,no presents with pains on the plantar surface and pelvic pain.!! ## !!5096=2013-07-29!! ## !!5219=1068!! ## !!6504=7189!! ## !!6509=6511!! ## !!6575=1107!! ## !!6605=1065!! ## !!7191=MELANOMA OF THE RIGHT HEEL.!! ## !!8723=5622!!'
bett = searchValue(obs, 1915)

print(bett)

任何帮助将不胜感激。

7 个答案:

答案 0 :(得分:1)

如果您对子字符串的位置不感兴趣,我会使用:

def searchValue(obs, concept):
    return re.findall('## !!'+ str(concept) + '=(.+?)!! ##',obs)

bett = searchValue(obs, 1915)
print(bett)

>>> ['Patient is awaiting imaging results, then start darcabazine 250 ml every 21 days.']


答案 1 :(得分:1)

您的代码中有2个错误:

  1. '## !!'concept中缺少 + (可能是拼写错误?)-产生语法错误的代码( SyntaxError
  2. 使用 int s( 1915 )添加字符串(`'## !!')-这是不可能的( TypeError )。您必须将 int 转换为 str

以下是您的模式(re.search的第一个 st 参数)的外观(最快的方法,当然,还有改进的余地):

>>> concept = 1915
>>> obs = '!!1834=7850!! ## !!1915=Patient is awaiting imaging results, then start darcabazine 250 ml every 21 days.!! ## !!1915=Patient is HIV positive since 2016,no presents with pains on the plantar surface and pelvic pain.!! ## !!5096=2013-07-29!! ## !!5219=1068!! ## !!6504=7189!! ## !!6509=6511!! ## !!6575=1107!! ## !!6605=1065!! ## !!7191=MELANOMA OF THE RIGHT HEEL.!! ## !!8723=5622!!'
>>>
>>> found = re.search('## !!' + str(concept) + '=(.+?)!! ##', obs)  # !!! Copy / paste this in your code
>>>
>>> found
<re.Match object; span=(14, 110), match='## !!1915=Patient is awaiting imaging results, th>
>>> found.group()
'## !!1915=Patient is awaiting imaging results, then start darcabazine 250 ml every 21 days.!! ##'

答案 2 :(得分:1)

在您的代码上进行了一些修改之后,我得到了一些看起来不错的东西;如果没有让我知道。

重要的部分是为re.search()提供完整的字符串;使用.format()完成。

问候


def searchValue(obs, concept):
    try:
        expression = '## !!{0}=(.+?)!! ##'.format(concept)
        found = re.search(expression, obs)
    except AttributeError:
        found = 'null'
    return found

obs= '!!1834=7850!! ## !!1915=Patient is awaiting imaging results, then start darcabazine 250 ml every 21 days.!! ## !!1915=Patient is HIV positive since 2016,no presents with pains on the plantar surface and pelvic pain.!! ## !!5096=2013-07-29!! ## !!5219=1068!! ## !!6504=7189!! ## !!6509=6511!! ## !!6575=1107!! ## !!6605=1065!! ## !!7191=MELANOMA OF THE RIGHT HEEL.!! ## !!8723=5622!!'
bett = searchValue(obs, 1915)

print(bett)

答案 3 :(得分:0)

您正在尝试将整数类型的字符串连接起来,这就是您收到错误的原因。 尝试使用此功能,并在解决问题后分享:

bett = searchValue(obs, str(1915))

还按照@CristiFati的建议在概念之前添加+号

答案 4 :(得分:0)

您错过了+。您还需要使要连接的整数(概念)成为字符串,因为您无法将整数与字符串连接。您还必须在“ obs”变量中将要搜索的1915设为字符串,因为“ obs”是不是整数的字符串。

U

答案 5 :(得分:0)

我想你是说:

    found = re.search('## !!' + concept + '=(.+?)!! ##',obs)

正确的版本是:

    found = re.search('## !!' + str(concept) + '=(.+?)!! ##',obs)

您必须将int强制转换为字符串。

答案 6 :(得分:0)

您的代码正确,但是您缺少concat的+

def searchValue(obs, concept):
try:
    found = re.search('## !!'+str(concept)+'=(.+?)!! ##',obs)
except AttributeError:
    found = 'null'
return found

obs= '!!1834=7850!! ## !!1915=Patient is awaiting imaging results, then start darcabazine 250 ml every 21 days.!! ## !!1915=Patient is HIV positive since 2016,no presents with pains on the plantar surface and pelvic pain.!! ## !!5096=2013-07-29!! ## !!5219=1068!! ## !!6504=7189!! ## !!6509=6511!! ## !!6575=1107!! ## !!6605=1065!! ## !!7191=MELANOMA OF THE RIGHT HEEL.!! ## !!8723=5622!!'
bett = searchValue(obs, 1915)

print(bett)

确保将整数值转换为字符串并传递,以便将其视为字符串或使用str(Integer)进行转换