简体   繁体   中英

How to concatenate Integers and strings in python

I am trying to search for a substring with the specify pattern using re library. I wrote a function to do that but end up getting this error: Type Error: cannot concatenate str and integers. 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:

  1. Missing a + in '## !!'concept (might be a typo?) - yielding syntactically incorrect code ( SyntaxError )
  2. Adding strings ( `'## !!' ) with int s ( 1915 ) - which is not possible ( TypeError ). You have to convert the int to str

Here's how your pattern ( re.search 's 1 st argument) should look like (quickest way, of course there's room for improvements):

 >>> 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; that's done using .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

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.

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.

Your code is correct but you are missing a + for 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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