0

When I try to print splited_data[1] I'm getting error message IndexError: list index out of range, On the other hand splited_data[0] is working fine. I want to insert data into MySQL. splited_data[0] are my MySQL columns and splited_data[1] is mysql column values. I want if splited_data[1] is empty then insert empty string in mysql. But I'm getting IndexError: list index out of range. How to avoid this error? Please help me. thank you

Here is my code. Which is working fine. I'm only get this error message when splited_data[1] is empty.

    def clean(data):
        data = data.replace('[[','')
        data = data.replace(']]','')
        data = data.replace(']','')
        data = data.replace('[','')
        data = data.replace('|','')
        data = data.replace("''",'')
        data = data.replace("<br/>",',')
        return data


for t in xml.findall('//{http://www.mediawiki.org/xml/export-0.5/}text'):
    m = re.search(r'(?ms).*?{{(Infobox film.*?)}}', t.text)
    if m:
        k =  m.group(1)
        k.encode('utf-8')
        clean_data = clean(k) #Clean function is used to replace garbase data from text
        filter_data = clean_data.splitlines(True) # splited data with lines
        filter_data.pop(0)
        for index,item in enumerate(filter_data):
                splited_data = item.split(' = ',1)
                print splited_data[0],splited_data[1]

# splited_data[0] used as mysql column 
# splited_data[1] used as mysql values

here is Splited_data data

[u' music         ', u'Jatin Sharma\n']
[u' cinematography', u'\n']
[u' released      ', u'Film datedf=y201124']

3 Answers 3

1
split_data = item.partition('=')
# If there was an '=', then it is now in split_data[1],
# and the pieces you want are split_data[0] and split_data[2].
# Otherwise, split_data[0] is the whole string, and
# split_data[1] and split_data[2] are empty strings ('').
Sign up to request clarification or add additional context in comments.

Comments

0

Try removing the whitespace on both sides of the equals sign, like this:

splited_data = item.split('=',1)

Comments

0

A list is contiguous. So you need to make sure it's length is greater than your index before you try to access it.

'' if len(splited_data) < 2 else splited_data[1]

You could also check before you split:

if '=' in item:
    col, val=item.split('=',1)
else:
    col, val=item, ''

Comments

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.