繁体   English   中英

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

[英]How to concatenate Integers and strings in python

我正在尝试使用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)

任何帮助将不胜感激。

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

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.']


您的代码中有2个错误:

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

这里有你的格局(怎么re.search1个变量)应该像(最快捷的方式,当然还有改进的余地):

 >>> 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.!! ##' 

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

重要的部分是为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)

您正在尝试使用整数类型连接字符串,这就是为什么您会收到错误消息。 尝试使用它,并在解决您的问题时分享:

bett = searchValue(obs, str(1915))

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

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

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, str(1915))

print(bett)

我想你的意思是:

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

正确的版本是:

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

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

您的代码是正确的,但是您缺少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)进行转换

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM