【问题标题】:Error While Performing NLTK on Spark RDD using spark-submit使用 spark-submit 在 Spark RDD 上执行 NLTK 时出错
【发布时间】:2020-10-07 05:16:55
【问题描述】:

我已将 pyspark_python 设置为 python3,我想在 Spark RDD 上执行 NLTK。 但是在执行 NLTK 时,它显示在错误下方。

  File "/home/user/.local/lib/python3.6/site-packages/nltk/corpus/reader/wordnet.py", line 1881, in <listcomp>
if form.endswith(old)
TypeError: endswith first arg must be bytes or a tuple of bytes, not str

当我在 HDP 集群上运行 spark 应用程序时效果很好,但在本地系统 spark-submit 上不起作用。

【问题讨论】:

  • 可以发布创建formold变量的代码吗?

标签: apache-spark pyspark nltk


【解决方案1】:

看起来 old 的类型是 str

old.encode() 会将其转换为字节

试试

if form.endswith(old.encode())

在 Python 3 中,unicode(str) 对象和 bytes 对象之间没有隐式转换。如果你知道输出的编码,你可以.decode()它得到一个字符串,或者你可以把你想添加到字节的\n"\n".encode('ascii')转成字节

所以你需要将 str 转换为字节的地方使用.encode() 以及需要将 byes 转换为 str 的地方使用.decode()

【讨论】:

  • 我正在使用以下代码在 Spark RDD 上执行 NLTK def lemma(x): 'import nltk` 'nltk.download('wordnet')` from nltk.stem import WordNetLemmatizer lemmatizer = WordNetLemmatizer() return lemmatizer.lemmatize(x) data = sc.textFile(hdfs_src_dir, use_unicode=False) @987654337 @ 我已经在 NLTK 库文件中应用了这个解决方案,但现在它给出了以下错误。 TypeError: can't concat str to bytes
  • 为什么你的代码中没有old? @AchyutVyas
  • 它是 NLTK 库的一部分。
猜你喜欢
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-07
相关资源
最近更新 更多