【问题标题】:How to create columns from a text file containing strings with spaces?如何从包含带空格的字符串的文本文件中创建列?
【发布时间】:2019-08-08 23:47:16
【问题描述】:

我想在 pyspark 中读取一个文件并从中创建一个数据框。它是一个 tsv 文件,其值格式为:

2015-07-22T09:00:28.019143Z "strings with space" "strings with space" ECDHE THRGH

我尝试创建行对象并将其转换为数据框。但我对如何拆分数据和创建列感到困惑。数据包含带空格的字符串。

from pyspark.sql import Row
from pyspark.sql import SparkSession
import re

spark = SparkSession \
    .builder \
    .appName("Task1") \
    .getOrCreate()

sc = spark.sparkContext

# Load a text file and convert each line to a Row.
lines = sc.textFile("file.tsv")
#parts = lines.map(lambda l: l.split(" ")) -- Tried this but it doesn't give correct output as there are spaces in strings.
values = lines.map(lambda p: Row(value=re.findall(r'\"(.+?)\"', str(p))[0].replace(" ","")))

理想的结果是如下的数据框:

Timestamp                          val1              val2      
 val3       val4
2015-07-22T09:00:28.019143Z strings with space strings with space ECDHE THRGH

【问题讨论】:

  • spark.read.option("delimiter", "\t").csv("path to file")
  • 感谢您的回答。但它没有给出预期的输出。

标签: python-3.x apache-spark pyspark apache-spark-sql


【解决方案1】:

要将任何分隔文件(asv)加载到 spark 中,您只需要提及分隔符即可。

df = spark.read.load("examples/src/main/resources/sample.txt",
                 format="csv", sep=" ", inferSchema="true", header="true")

在上述行中使用 sep 属性中的空格。

已编辑:

如果文件没有任何标题,请尝试以下更改列名。同时标记 header="false"

df = spark.read.load("examples/src/main/resources/sample.txt",
                 format="csv", sep=" ", inferSchema="true", header="false").toDF("id","name", "salary")

【讨论】:

  • 似乎工作正常。谢谢。读取文件时如何在数据中添加标题?我的文件中没有标题。
  • 编辑了我的答案,请检查
【解决方案2】:

这是我的 tsv 文件,但实际上是用空格分隔的。

timestamp col1 col2 col3 col4
2015-07-22T09:00:28.019143Z "strings with space" "strings with space" ECDHE THRGH
2015-07-22T09:00:28.019143Z "strings with space" "strings with space" ECDHE THRGH
2015-07-22T09:00:28.019143Z "strings with space" "strings with space" ECDHE THRGH
2015-07-22T09:00:28.019143Z "strings with space" "strings with space" ECDHE THRGH
2015-07-22T09:00:28.019143Z "strings with space" "strings with space" ECDHE THRGH

用我下面的代码,

df = spark.read.option("delimiter", " ").option("header", "true").csv("path/to/tsv")
df.show(7, False)

结果如下图。

+---------------------------+------------------+------------------+-----+-----+
|timestamp                  |col1              |col2              |col3 |col4 |
+---------------------------+------------------+------------------+-----+-----+
|2015-07-22T09:00:28.019143Z|strings with space|strings with space|ECDHE|THRGH|
|2015-07-22T09:00:28.019143Z|strings with space|strings with space|ECDHE|THRGH|
|2015-07-22T09:00:28.019143Z|strings with space|strings with space|ECDHE|THRGH|
|2015-07-22T09:00:28.019143Z|strings with space|strings with space|ECDHE|THRGH|
|2015-07-22T09:00:28.019143Z|strings with space|strings with space|ECDHE|THRGH|
|2015-07-22T09:00:28.019143Z|strings with space|strings with space|ECDHE|THRGH|
|2015-07-22T09:00:28.019143Z|strings with space|strings with space|ECDHE|THRGH|
+---------------------------+------------------+------------------+-----+-----+

看起来效果不错。

【讨论】:

  • 实际上我在字段之间没有标签。所有字段都由空格本身分隔。加上“带空格的字符串”有空格。
  • 我将标题选项更改为 df = spark.read.option("delimiter", " ") 并且它起作用了。我不知道为什么。
  • 是的,它似乎正在工作。如何在此处向此数据框添加标头。有没有什么我可以写在你给出的相同声明中的选项。
  • 如果文件顶部有标题,当我将文件加载到数据框时,我会添加标题为 true 的选项。
  • 或者如果你没有标题,然后尝试在最后添加选项,例如 .toDF("col1","col2","col3")
猜你喜欢
  • 2011-03-14
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多