Liraz Rubinstein
Liraz Rubinstein

Reputation: 21

split cell to rows by pipe python

I am trying to split a cell into 2 rows by pipe ("|").

For instance:

ID  Site    Category    Queries

1   0        38037      antique+wall+telephone|antique+wall+phone

will become:

ID  Site    Category    Queries

1   0         38037     antique+wall+telephone

1   0         38037     antique+wall+phone

Upvotes: 2

Views: 85

Answers (3)

Corentin
Corentin

Reputation: 2552

Using talend, You can also use tNormalize component : you just have to specify the column to normalize in the dropdown menu. Be careful if you want to use "|" as the item separator, as it is a reserved character, you have to escape it using "\|". enter image description here

Upvotes: 0

holdenweb
holdenweb

Reputation: 37053

Here's one way:

>>> id, site, category, queries
('1', '0', '38037', 'antique+wall+telephone|antique+wall+phone')
>>> for query in queries.split('|'):
...     print id, site, category, query
... 
1 0 38037 antique+wall+telephone
1 0 38037 antique+wall+phone

Upvotes: 2

pladder
pladder

Reputation: 142

You can use .split()

input = "antique+wall+telephone|antique+wall+phone"
output = input.split('|')

output will be a list of elements either side of the '|'

so output would be

("antique+wall+telephone", "antique+wall+phone")

and then you can simply add the ID Number in front of each element of the list of strings

Hope that answers your question

Upvotes: 0

Related Questions