Python Forum
Python split and concatenate
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python split and concatenate
#1
I am new to Python and using python 2.7 version

I am trying to extract array column name using python.

Array column is mentioned below:
`col` array<struct< columnname:string,columnname1:int,columnname2:decimal(10,0),
columnname3:decimal(9,2)>>
I am spliting the column using split operator
What I tried so far:

import re
str=input("enter any string:")
fields=str.split(",")
for x in fields:
  name=x.split(":")
  seminame=name[0]+','
  firstname=seminame.find('`')
  lastname=seminame.rfind('`')
  fullname=seminame[(firstname+1):lastname]
  replacename1=fullname.replace(')', '')
  replacename2=fullname.replace('2', '')
  replacename3=fullname.replace('9', '')
  replacename4=fullname.replace('10', '')
  replacename5=fullname.replace('0', '')
  finalname='.'+replacename5
  print(finalname)
---------------------------------------------------------------------------------------------------------------

Input:
'`col` array<struct< columnname:string,columnname1:int,columnname2:decimal(10,0),
columnname3:decimal(9,2)>>'
I want the output as

Actual output
.col,
.columnname1,
.columnname2,
.),

Expected output
col.columnname,
col.columnname1,
col.columnname2,
col.columnname3


Saravanan
Reply
#2
decimal(10,0) - the comma is a problem

a short test with decimal(10.0)

#mylist = ["columnname:string,columnname1:int,columnname2:decimal(10,0),columnname3:decimal(9,2)"]
mylist = ["columnname:string,columnname1:int,columnname2:decimal(10.0),columnname3:decimal(9.2)"]
 
for a in ",".join(mylist).split(","):
    name = "%s%s" % ("col.", a.partition(":")[0]) 
    print(name)
Output:
col.columnname col.columnname1 col.columnname2 col.columnname3
Reply
#3
Hi Axel,

Thanks for your reply. I tried your program

mylist = ["`col` array<struct< columnname:string,columnname1:int,columnname2:decimal(10,0),columnname3:decimal(9,2)>>"]
for a in ",".join(mylist).split(","):
    name = "%s%s" % ("col.", a.partition(":")[0]) 
    print(name)
I am not getting desired output.
Output:
Actual Output: col.`col` array<struct< columnname col.columnname1 col.columnname2 col.0) col.columnname3 col.2)>>
Output:
Expected Output: col.columnname, col.columnname1, col.columnname2, col.columnname3
Reply
#4
1. There are some things wrong with your list, how did you get the list? (is it sql?)

`col` array<struct< 
can not be part of the list.

2. as i said before the comma is a problem (9,2),it would be easy with a point (9.2)

maybe this works (but there must be a better way)

import re
mylist = ["`col` array<struct< columnname:string,columnname1:int,columnname2:decimal(10,0),columnname3:decimal(9,2)>>"]
s = ",".join(mylist).replace("`col` array<struct< ", "").replace(">>", "").split(",")
n = (re.split(r',\s*(?![^()]*\))', str(s)))
for a in n:
    name = "%s%s" % ("col.", str(a.partition(":")[0]).replace("'", "").replace("[", ""))
    print(name)
Output:
col.columnname col.columnname1 col.columnname2 col.columnname3
Reply
#5
col array<struct< ---> It is SQL. Thanks Axel.
Reply
#6
(Jul-31-2018, 08:22 AM)saravanatn Wrote: col array<struct< ---> It is SQL. Thanks Axel.

then it would be better to use sql to read it. Do you have an example file?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concatenate str JohnnyCoffee 2 2,879 May-01-2021, 03:58 PM
Last Post: JohnnyCoffee
  Concatenate two dataframes moralear27 2 1,835 Sep-15-2020, 08:04 AM
Last Post: moralear27
  can only concatenate str (not "int") to str gr3yali3n 6 4,022 May-28-2020, 07:20 AM
Last Post: pyzyx3qwerty
  Concatenate multiple PDFs using python gmehta1996 0 2,083 Mar-29-2020, 09:48 PM
Last Post: gmehta1996
  Concatenate two dictionaries harish 3 2,322 Oct-12-2019, 04:52 PM
Last Post: strngr12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020