安装Spark与Python练习

一、安装Spark

  1. 检查基础环境hadoop,jdk


  2. 下载spark

    之前课程已经做好

  3. 解压,文件夹重命名、权限

    之前课程已经做好

  4. 配置文件

  5. 环境变量

  6. 试运行Python代码

二、Python编程练习:英文文本的词频统计

    1. 准备文本文件
    2. 读文件
    3. 预处理:大小写,标点符号,停用词
    4. 分词

    5. 统计每个单词出现的次数

    6. 按词频大小排序

    7. 结果写文件

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      path='/home/hadoop/wc/111.txt'
      with open(path) as f:
          text=f.read()
       
      text = text.lower()
      text = text.replace(",", " ").replace(".", " ").replace("!", " ")
      text = text.strip()
      words = text.split()
       
      wc={}
      for word in words:
          wc[word]=wc.get(word,0)+1
      wclist=list(wc.items())
      wclist.sort(key=lambda x:x[1],reverse=True)
      txt = open("222.txt","w",encoding='UTF-8')
      txt.write(str(wclist))
      print(wclist)

        



posted @ 2022-03-06 14:12  蔡念威  阅读(57)  评论(0)    收藏  举报