2.安装Spark与Python练习
一、安装Spark
(1)检查基础环境hadoop,jdk
(2)配置文件
vim /usr/local/spark/conf/spark-env.sh

(3)环境变量
vim ~/.bashrc


(4)试运行Python代码

二、Python编程练习:英文文本的词频统计
(1)准备文本文件(txt)
(2)读文件
open(r'D:\桌面\countWord\waerdenghu.txt','rb')
input_text.read()
(3)预处理
去除标点符号并转换成小写:word.strip(string.punctuation).lower()
去重:words_index = set(words)
(4)分词
text.read().split()
(5)储存单词以及单词出现的次数:
count_dict = {index:words.count(index) for index in words_index}
(6)按词频大小排序
字典中的值为排序的参数:
for word in sorted(counts_dict,key=lambda x: counts_dict[x],reverse=True)
(7)结果写文件
output_text.writelines('{}--{} times'.format(word,count_dict[word]) + '\n')
(8)运行结果


点击查看源代码
import string
from os import path
with open(r'D:\桌面\countWord\waerdenghu.txt','rb') as input_text:
words = [word.strip(string.punctuation).lower() for word in str(input_text.read()).split()]
words_index = set(words)
count_dict = {index:words.count(index) for index in words_index}
with open(r'D:\桌面\countWord\hwtcount.txt','a+') as output_text:
output_text.writelines('词频统计的结果为:' + '\n')
for word in sorted(count_dict,key=lambda x:count_dict[x],reverse=True):
output_text.writelines('{}--{} times'.format(word,count_dict[word]) + '\n')
input_text.close()
output_text.close()
三、根据自己的编程习惯搭建编程环境
使用PyCharm


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通