要读取csv中带有逗号和引号的字段,其中逗号是分隔符pysp

2024-05-28 20:09:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我在我的输入csv文件中有一个记录,是

"2017-11-01","2017-10-29","2017-11-04","4532491","","","","Natural States: "The Environmental Imagination" in Maine, Oregon, and the Nation","1000","Richard W. Judd"

当我在pyspark中读到这个csv时,字段"Natural States: "The Environmental Imagination" in Maine, Oregon, and the Nation"被分隔为单独的列。在

^{pr2}$

除了更改输入文件中的分隔符之外的任何解决方法,因为我们无法更改输入文件。在


Tags: and文件csvtheinrichard记录natural
2条回答

您可以使用sparkContext读取文件,并使用多个字符","读取文件,然后将{}转换为{},如下所示

rdd = sc.textFile("file.csv")

def replaceFunc(words):
    result = []
    for word in words.split("\",\""):
        result.append(word.replace("\"", ""))
    return result

rdd.map(replaceFunc).toDF().show(1, False)

您应该有以下输出

^{pr2}$

这可能与sep='","'一起工作,例如:

spark.read.csv('file.csv', sep='","')

相关问题 更多 >

    热门问题