简体   繁体   English

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

[英]How to concatenate Integers and strings in python

I am trying to search for a substring with the specify pattern using re library. 我正在尝试使用re库搜索具有指定模式的子字符串。 I wrote a function to do that but end up getting this error: Type Error: cannot concatenate str and integers. 我编写了一个函数来执行此操作,但最终收到此错误:类型错误:无法连接str和整数。 Below is my function; 下面是我的功能;

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)

Any help will be appreciated. 任何帮助将不胜感激。

If you are not interested in substrings position I'd use: 如果您对子字符串位置不感兴趣,请使用:

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


There are 2 errors in your code: 您的代码中有2个错误:

  1. Missing a + in '## !!'concept (might be a typo?) - yielding syntactically incorrect code ( SyntaxError ) 缺少'## !!'concept+ (可能是拼写错误?)-产生语法错误的代码( SyntaxError
  2. Adding strings ( `'## !!' ) with int s ( 1915 ) - which is not possible ( TypeError ). 使用int s( 1915 )添加字符串( ''## !!' )-这是不可能的( TypeError )。 You have to convert the int to str 您必须将int转换为str

Here's how your pattern ( re.search 's 1 st argument) should look like (quickest way, of course there's room for improvements): 这里有你的格局(怎么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.!! ##' 

after some wortk on your code i got something that seems fine; 在您的代码上进行了一些修改之后,我得到了一些看起来不错的东西; if not let me know. 如果没有让我知道。

The important part is to feed the re.search() with a complete string; 重要的部分是为re.search()提供完整的字符串。 that's done using .format(). 使用.format()完成。

Greetings 问候


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)

You are trying to concatenate a string with integer type that's why you are getting the error. 您正在尝试使用整数类型连接字符串,这就是为什么您会收到错误消息。 try using this and do share if it solves your problem: 尝试使用它,并在解决您的问题时分享:

bett = searchValue(obs, str(1915))

also add the + sign before concept as suggested by @CristiFati 还按@CristiFati的建议在概念之前添加+号

You missed a +. 您错过了+。 You also need to make the integer that you are trying to concatenate (concept) a string because you cannot concatenate integers with strings. 您还需要使要连接的整数(概念)成为字符串,因为您无法将整数与字符串连接。 You must also make the 1915 you are searching for in the 'obs' variable a string because 'obs' is a string not an integer. 您还必须使要搜索的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)

I think you mean: 我想你的意思是:

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

The correct version is: 正确的版本是:

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

You have to cast from int to string explicitly. 您必须将int强制转换为字符串。

Your code is correct but you are missing a + for concat 您的代码是正确的,但是您缺少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)

Make sure you convert the integer value to string and pass so that it considers it as a string or convert it using str(Integer) 确保将整数值转换为字符串并传递,以便将其视为字符串或使用str(Integer)进行转换

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

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