1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python 去除字符串的标点符号 用_Python输入和输出

python 去除字符串的标点符号 用_Python输入和输出

时间:2021-11-07 22:13:34

相关推荐

python 去除字符串的标点符号 用_Python输入和输出

点击上方Python知识圈,设为星标

回复1024获取Python资料

阅读文本大概需要 5分钟

近期精彩文章Python100例(附PDF下载地址)

在很多时候,你会想要让你的程序与用户(可能是你自己)交互。你会从用户那里得到输入,然后打印一些结果。我们可以使用input和print语句来完成这些功能。

input

name=input('yourname:')

gender=input('youareaboy?(y/n)')

######输入######

yourname:Runsen

you are a boy?:y

welcome_str='Welcometothematrix{prefix}{name}.'

welcome_dic={

'prefix':'Mr.'ifgender=='y'else'Mrs',

'name':name

}

print('authorizing...')

print(welcome_str.format(**welcome_dic))

##########输出##########

authorizing...

WelcometothematrixMr.Runsen.

input函数暂停运行,等待键盘输入,直到按下回车,输入的类型永远时字符串

a=input()

1

b=input()

2

print('a+b={}'.format(a+b))

##########输出##############

a+b=12

print('typeofais{},typeofbis{}'.format(type(a),type(b)))

##########输出##############

typeofais<class'str'>,typeofbis<class'str'>print('a+b={}'.format(int(a)+int(b)))

##########输出##############a+b=3

文件输入和输出

生产级别的 Python 代码,大部分 I/O 则来自于文件

这里有个in.text,完成worldcount功能。

Mr.Johnsonhadneverbeenupinanaerophanebeforeandhehadreadalotaboutairaccidents,soonedaywhenafriendofferedtotakehimforarideinhisownsmallphane,Mr.Johnsonwasveryworriedaboutaccepting.Finally,however,hisfriendpersuadedhimthatitwasverysafe,andMr.Johnsonboardedtheplane.

Hisfriendstartedtheengineandbegantotaxiontotherunwayoftheairport.Mr.Johnsonhadheardthatthemostdangerouspartofaflightwerethetake-offandthelanding,sohewasextremelyfrightenedandclosedhiseyes.

Afteraminuteortwoheopenedthemagain,lookedoutofthewindowoftheplane,andsaid to his friend。

"Lookatthosepeopledownthere.Theylookassmallasants,don'tthey?"

"Thoseareants,"answeredhisfriend."We'restillontheground."

现在

读取文件去掉所有标点和换行符,将大写变为小写合并相同的词,统计每个词出现的频率,将词频从大到小排序将结果按行输出文件out.txt

importre

#你不用太关心这个函数

defparse(text):

#使用正则表达式去除标点符号和换行符

text=re.sub(r'[^\w]','',text)

#转为小写

text=text.lower()

#生成所有单词的列表

word_list=text.split('')

#去除空白单词

word_list=filter(None,word_list)

#生成单词和词频的字典

word_cnt={}

forwordinword_list:

ifwordnotinword_cnt:

word_cnt[word]=0

word_cnt[word]+=1

#按照词频排序

sorted_word_cnt=sorted(word_cnt.items(),key=lambdakv:kv[1],reverse=True)

returnsorted_word_cnt

withopen('in.txt','r')asfin:

text=fin.read()

word_and_freq=parse(text)

withopen('out.txt','w')asfout:

forword,freqinword_and_freq:

fout.write('{}{}\n'.format(word,freq))

##########输出(省略较长的中间结果)##########

但是有个问题,如果文件非常的大容易造成内存奔溃

这个时候给 read 指定参数 size,还可以通过 readline() 函数,每次读取一行。

json文件读取

importjson

params={

'symbol':'123456',

'type':'limit',

'price':123.4,

'amount':23

}

params_str=json.dumps(params)

print('afterjsonserialization')

print('typeofparams_str={},params_str={}'.format(type(params_str),params))

original_params=json.loads(params_str)

print('afterjsondeserialization')

print('typeoforiginal_params={},original_params={}'.format(type(original_params),original_params))

##########输出##########

afterjsonserialization

typeofparams_str=<class'str'>,params_str={'symbol':'123456','type':'limit','price':123.4,'amount':23}

afterjsondeserialization

typeoforiginal_params=<class'dict'>,original_params={'symbol':'123456','type':'limit','price':123.4,'amount':23}

json.dumps() 这个函数,接受 Python 的基本数据类型 字典,然后转化string (json的字符串)

json.loads() 这个函数,接受一个合法字符串(json),然后 转化为字典

「json 的读入」

importjson

params={

'symbol':'123456',

'type':'limit',

'price':123.4,

'amount':23

}

withopen('params.json','w')asfout:

params_str=json.dump(params,fout)

withopen('params.json','r')asfin:

original_params=json.load(fin)

print('afterjsondeserialization')

print('typeoforiginal_params={},original_params={}'.format(type(original_params),original_params))

##########输出##########

afterjsondeserialization

typeoforiginal_params=<class'dict'>,original_params={'symbol':'123456','type':'limit','price':123.4,'amount':23}

参考:/column/article/96570

-----------------------公众号:Python知识圈博客知乎:Python知识圈微信视频号:菜鸟程序员 (分享有趣的编程技巧、Python技巧)bilibili:菜鸟程序员的日常(目前原创视频:22,累计播放量:85万)

我的微信视频号定时更新中,近期真人出镜分析讲解 Python 经典习题,后续会分享更多的干货,欢迎关注我的微信视频号。

Python知识圈公众号的交流群已经建立,群里可以领取 Python 相关学习资料,大家可以一起学习交流,效率更高,如果是想发推文、广告、砍价小程序的敬请绕道!一定记得备注「交流学习」,不然不会通过好友。

扫码添加,备注:交流学习

往期推荐01

公众号所有文章汇总导航(2-10更新)

02

Python100例(附PDF下载地址)

03

打基础一定要吃透这12类 Python 内置函数

↓点击阅读原文查看pk哥原创视频

我就知道你“在看”

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。