0

i'm trying to convert string to integer, but it's not that so easier than i'm thinking.

content = '''
<entry colname="1" morerows="1" morerowname="2"><p>111</p></entry>
<entry colname="2" rowname="2"><p></p></entry>'''


morerows = ''.join(re.findall('morerows="\d"', content))
morerows_n = int(''.join(re.findall('\d', morerows)))
print(morerows_n)

this results error as follow :

morerows_n = int(''.join(re.findall('\d', morerows)))
ValueError: invalid literal for int() with base 10: ''

where is wrong with that code? i've tried int() function but doesn't work and it's not float also.

any help?

1
  • 1
    It results in 1 for me Commented Nov 22, 2018 at 4:50

1 Answer 1

1

I guess there are non-integer characters in the morerows attribute in your real case.

How about this:

content = '''
<entry colname="1" morerows="1x" morerowname="2"><p>111</p></entry>
<entry colname="1" morerows="1" morerowname="2"><p>111</p></entry>
<entry colname="2" rowname="2"><p></p></entry>'''

morerows = ''.join(re.findall('morerows="[0-9]+"', content))
if morerows:
    morerows_n = int(''.join(re.findall('\d', morerows)))
print(morerows_n)

Use [0-9]+ instead of \d

Sign up to request clarification or add additional context in comments.

2 Comments

I found what it's wrong with , yes you're right! some attributes have no morerows=" attribute in real case. so i use try and except. lot of thanks!
and i guess it's better to use \d than [0-9] cuz this number can be over 10. anyway thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.