0

我有一个保存在没有标题的文本文件中的字符串列表,并且想在数据块中的 pyspark 笔记本中打开并打印所有行。

abcdef 
vcdfgrs 
vcvdfrs 
vfdedsew 
kgsldkflfdlfd

text = sc.textFile("path.../filename.txt)
print(text.collect()) 

此代码不打印行。我感谢您的帮助。

4

1 回答 1

1

来了

#define a function which takes line and print
def f(line):
    print(line)

#building the text file via list
my_list = [['my text line-1'],['line-2 text2 my2'],['some junk line-3']]

#create RDD via list (you have it via 
txt_file = sc.parallelize(my_list)

#use for each to call the function and print will work
txt_file.foreach(f)

#if you want each word via line, use flatmap

在此处输入图像描述

于 2020-04-16T05:00:55.477 回答