简体   繁体   中英

how to convert list into string in python

I'm getting output as list value as shown below [Row(column1='a,b,c,d')]

how to convert this to string value needed output: 'a,b,c,d'

how to achieve this using python/pyspark?

data= [ ('a,b,c,d',), ]
df = spark.createDataFrame(data, ['column1'])

print(df.first()) # OR print(df.take(1))
# [Row(column1='a,b,c,d')]
res = df.first()["column1"] # OR res = df.take(1)[0]["column1"]
print(type(res))
# <class 'str'>
print(res)
# a,b,c,d

What you're referring to is just a list of PySpark Row objects, and in order to get values out of those objects, you just need to loop through it

print([r['column1'] for r in df.collect()])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM