Python Forum
Python split and concatenate - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Python split and concatenate (/thread-11877.html)



Python split and concatenate - saravanatn - Jul-30-2018

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


RE: Python split and concatenate - Axel_Erfurt - Jul-30-2018

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



RE: Python split and concatenate - saravanatn - Jul-30-2018

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



RE: Python split and concatenate - Axel_Erfurt - Jul-30-2018

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



RE: Python split and concatenate - saravanatn - Jul-31-2018

col array<struct< ---> It is SQL. Thanks Axel.


RE: Python split and concatenate - Axel_Erfurt - Jul-31-2018

(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?