1

I have an array of string like this

[u'ROWKEY\ufffdACCOUNTID\ufffdACCOUNTIDDSC']

How do i convert the above list into the below array list in python

['ROWKEY','ACCOUNTID','ACCOUNTIDDSC']

4 Answers 4

3

You should encode your string not decode. Your provided list (array of string as you mentioned) consists of a unicode sting. To represent a unicode string as a string of bytes is known as encoding, use u'...'.encode(encoding). Then by using string.split() you can break that encoded string down into smaller chunks, or strings.

lst = [u'ROWKEY\ufffdACCOUNTID\ufffdACCOUNTIDDSC']
new_list = [i.encode('utf8') for i in lst[0].split(u'\ufffd')]
print(new_list)

Output would be:

['ROWKEY', 'ACCOUNTID', 'ACCOUNTIDDSC']
Sign up to request clarification or add additional context in comments.

Comments

1

Do it like this:

old_list = [u'ROWKEY\ufffdACCOUNTID\ufffdACCOUNTIDDSC']
new_list = old_list[0].split(u'\ufffd')
print(new_list)

Hope it helps.

Comments

1

Use str.split()

>>> [u'ROWKEY\ufffdACCOUNTID\ufffdACCOUNTIDDSC'][0].split(u"\ufffd")
[u'ROWKEY', u'ACCOUNTID', u'ACCOUNTIDDSC']

Comments

1

Using Regex. re.split

Ex:

import re

l = u'ROWKEY\ufffdACCOUNTID\ufffdACCOUNTIDDSC'
print(re.split(r"[^a-zA-Z]", l))

Output:

[u'ROWKEY', u'ACCOUNTID', u'ACCOUNTIDDSC']

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.