1

I've got this string:

string = '26.10-8-00 - Fabricação de componentes eletrônicos | 46.51-6-01 - Comércio atacadista de equipamentos de informática | 95.11-8-00 - Reparação e manutenção de computadores e de equipamentos periféricos'

I want to create a list with those codes out of it. It might look like this:

mylist = ['26.10-8-00', '46.51-6-01', '95.11-8-00']
1
  • 2
    using regular exression? re.findall(r'[0-9]+.[0-9]+-[0-9]-[0-9]+', string) Commented Oct 3, 2017 at 23:54

4 Answers 4

1

using re.findall

import re
string = '26.10-8-00 - Fabricação de componentes eletrônicos | 46.51-6-01 - Comércio atacadista de equipamentos de informática | 95.11-8-00 - Reparação e manutenção de computadores e de equipamentos periféricos'
output = re.findall(r'\d+\.\d+-\d+-\d+', string)
# ['26.10-8-00', '46.51-6-01', '95.11-8-00']
Sign up to request clarification or add additional context in comments.

Comments

1

I'd split the string by | to get a list, and then split each value by - and extract just the date part of it:

result = [x.split(' - ')[0] for x in s.split(' | ')]

Comments

1

You can use regular expressions:

import re
string = '26.10-8-00 - Fabricação de componentes eletrônicos | 46.51-6-01 - Comércio atacadista de equipamentos de informática | 95.11-8-00 - Reparação e manutenção de computadores e de equipamentos periféricos'
new_string = [i for i in re.split("\s-\s|(?<=\|)\s(?=\d)", string) if re.findall("^\d+\.\d+-\d+-\d+", i)]

Output:

['26.10-8-00', '46.51-6-01', '95.11-8-00']

Comments

1

Mines a more simple approach but gets the job done.

string = '26.10-8-00 - Fabricação de componentes eletrônicos | 46.51-6-01 - Comércio atacadista de equipamentos de informática | 95.11-8-00 - Reparação e manutenção de computadores e de equipamentos periféricos'
mylist = []
for x in string.split(" | "):
  mylist.append(x.split(" - ")[0])
print(mylist)

Output

['26.10-8-00', '46.51-6-01', '95.11-8-00']

2 Comments

Consider .strip() to remove the leading space in the last two results.
i fixed it by adding spaces infront and behind the | in "for x in string.split(" | "):"

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.