python - 如何从pyspark中的字符串中删除特定字符?
问题描述
我正在尝试从字符串中删除特定字符,但无法获得任何适当的解决方案。你能帮我怎么做吗?
我正在使用 pyspark 将数据加载到数据框中。具有我要删除的额外字符的列之一。
例子:
|"\""warfarin was discontinued 3 days ago and xarelto was started when the INR was 2.7, and now the INR is 5.8, should Xarelto be continued or stopped?"|
但结果我只想要:
|"warfarin was discontinued 3 days ago and xarelto was started when the INR was 2.7, and now the INR is 5.8, should Xarelto be continued or stopped?"|
我正在使用以下代码将数据帧写入文件:
df.repartition(1).write.format('com.databricks.spark.csv').mode('overwrite').save(output_path, escape='\"', sep='|',header='True',nullValue=None)
解决方案
您可以使用其他一些转义字符而不是“\”,您可以将其更改为其他任何字符。如果您可以选择将文件保存为任何其他格式,则首选 parquet(或 orc)而不是 csv。
推荐阅读
- python - 从 Yaml 文件中读取正则表达式字符串会使反斜杠加倍
- python - Tweepy、高级 API 和速率限制 - 在不到 10 分钟内达到 250 个请求,几乎没有 10,000 条推文
- node.js - findOneAndUpdate() 用于两个不同的 ObjectId,但只更新一个
- python - Python - 错误:lib_type = fields[4] IndexError:列表索引超出范围
- typescript - Typescript中命名元组的通用名称是什么?
- json - 为什么 JSONDecoder 总是为可选属性返回 nil?
- python - AttributeError:“PathCollection”对象没有属性“markeredgecolor”
- reactjs - 在不与我的元素相交后如何保留我的更改?
- javascript - 在javascript中定义一个枚举对象并从它的键中获取字符串
- java - Java InputStream 的读取方法没有读取前几个字节的问题