В Python
мы можем использовать регулярное выражение split
, мы разбиваем данные на основе несовместимых пробелов.
import re
re.split("\\s+",'a b c')
['a', 'b', 'c']
In Pyspark:
#sample data
$ cat i.txt
one two three four five
six seven eight nine ten
cols=["col1","col2","col3","col4","col5"]
spark.sparkContext.textFile("<file_path>/i.txt").map(lambda x:re.split("\\s+",x)).toDF(cols).show()
#creating dataframe on the file with inconsistent spaces.
#+----+-----+-----+----+----+
#|col1| col2| col3|col4|col5|
#+----+-----+-----+----+----+
#| one| two|three|four|five|
#| six|seven|eight|nine| ten|
#+----+-----+-----+----+----+