Tuesday, January 3, 2023

How do I transform a text data with cells in a rows separated by pipe sign into specific pattern of data via python?

import io

text = """A | B | Lorem | Ipsum | is | simply | dummy
C | D | text | of | the | printing | and
E | F | typesetting | industry. | Lorem
G | H | more | recently | with | desktop | publishing | software | like | Aldus
I | J | Ipsum | has | been | the | industry's
K | L | standard | dummy | text | ever | since | the | 1500s
M | N | took | a
O | P | scrambled | it | to | make | a | type | specimen | book"""
for line in io.StringIO(text):
row = line.strip().split(' | ')
for i in range(2, len(row), 2):
print(' | '.join(row[:2] + row[i: i+2]))

output :

A | B | Lorem | Ipsum

A | B | is | simply

A | B | dummy

C | D | text | of

C | D | the | printing

C | D | and

E | F | typesetting | industry.

E | F | Lorem

G | H | more | recently

G | H | with | desktop

G | H | publishing | software

G | H | like | Aldus

I | J | Ipsum | has

I | J | been | the

I | J | industry's

K | L | standard | dummy

K | L | text | ever

K | L | since | the

K | L | 1500s

M | N | took | a

O | P | scrambled | it

O | P | to | make

O | P | a | type

O | P | specimen | book


Process finished with exit code 0


Recent Post

Databricks Delta table merge Example

here's some sample code that demonstrates a merge operation on a Delta table using PySpark:   from pyspark.sql import SparkSession # cre...