PySpark获取Dataframe中所有非ASCII字符

python 复制代码
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, concat_ws, explode, split, coalesce, lit
from pyspark.sql.types import StringType

spark = SparkSession.builder.appName("InvalidCharacterFinder").getOrCreate()

# 假设已存在DataFrame df
# df = ...

# 获取所有字符串类型列名
string_columns = [f.name for f in df.schema.fields if isinstance(f.dataType, StringType)]
result = []

if string_columns:
    # 处理空值并合并字符串列
    non_null_cols = [coalesce(col(c), lit("")).alias(c) for c in string_columns]
    combined_df = df.select(non_null_cols).select(concat_ws("", *string_columns).alias("merged_str"))
    
    # 拆分字符并过滤空字符串
    chars_df = combined_df.withColumn("char", explode(split(col("merged_str"), "")))\
                          .filter(col("char") != "")
    
    # 定义合法字符集合
    allowed_chars = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!'
                        '"#$%&\'()*+,-./:;<=>?@[]^_`{|}~')
    
    # 收集非法字符并去重
    unique_invalid = chars_df.rdd.map(lambda x: x.char)\
                             .filter(lambda c: c not in allowed_chars)\
                             .distinct()\
                             .collect()
    
    # 按首次出现顺序保留字符(分布式环境无法保证绝对顺序)
    seen = set()
    ordered_result = []
    for char in unique_invalid:
        if char not in seen:
            ordered_result.append(char)
            seen.add(char)
    result = ordered_result

print("非法字符集合:", ''.join(result))

代码说明:

  1. 数据准备:通过DataFrame Schema识别所有字符串类型的列
  2. 空值处理 :使用coalesce函数将NULL转换为空字符串,确保后续字符串合并有效
  3. 列合并 :使用concat_ws将多个字符串列的值合并为单个字符串
  4. 字符拆分:通过split+explode将字符串拆分为单个字符,并过滤空字符
  5. 非法字符过滤:使用RDD操作过滤不在白名单中的字符,并通过distinct去重
  6. 结果处理:使用有序集合保持字符首次出现的顺序(注意:分布式环境下无法保证绝对顺序)

注意事项:

  • 最终结果字符顺序可能与实际数据中的出现顺序不完全一致
  • 白名单包含94个可打印ASCII字符(排除空格和控制字符)
  • 使用RDD操作提升分布式处理性能
  • 最终结果字符串可能包含各类特殊符号、中文、表情符号等非标准ASCII字符
相关推荐
成长之路5141 小时前
【工具变量】最新华证ESG评级得分数据-含xlsx及dta格式(2009-2024.12)
大数据
满怀10151 小时前
【Python】os模块
开发语言·python
三道杠卷胡1 小时前
【AI News | 20250507】每日AI进展
人工智能·python·计算机视觉·语言模型·aigc
胡耀超1 小时前
对称加密算法(AES、ChaCha20和SM4)Python实现——密码学基础(Python出现No module named “Crypto” 解决方案)
开发语言·python·密码学·数据安全·aes·sm4·chacha
巴拉特好队友2 小时前
说说es配置项的动态静态之分和集群配置更新API
大数据·elasticsearch·搜索引擎
End9282 小时前
MapReduce中的分区器
大数据·hadoop
小Tomkk2 小时前
怎么在非 hadoop 用户下启动 hadoop
大数据·hadoop·问题
极小狐2 小时前
极狐GitLab 如何将项目共享给群组?
大数据·数据库·elasticsearch·机器学习·gitlab
io_T_T3 小时前
(dify)如何使用dify自定义知识库【dify外部链接知识库】
python