日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2024/1/23 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 去除字符串的标点符号 用_Python输入和输出 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

點擊上方Python知識圈,設為星標

回復1024獲取Python資料

閱讀文本大概需要 5?分鐘

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

在很多時候,你會想要讓你的程序與用戶(可能是你自己)交互。你會從用戶那里得到輸入,然后打印一些結果。我們可以使用input和print語句來完成這些功能。

input

name?=?input('your?name:')
gender?=?input('you?are?a?boy?(y/n)')

######?輸入?######
your?name:Runsen
you are a boy?:y

welcome_str?=?'Welcome?to?the?matrix?{prefix}?{name}.'
welcome_dic?=?{
????'prefix':?'Mr.'?if?gender?==?'y'?else?'Mrs',
????'name':?name
}

print('authorizing...')
print(welcome_str.format(**welcome_dic))

##########?輸出?##########
authorizing...
Welcome?to?the?matrix?Mr.?Runsen.

input函數暫停運行,等待鍵盤輸入,直到按下回車,輸入的類型永遠時字符串

a?=?input()
1
b?=?input()
2

print('a?+?b?=?{}'.format(a?+?b))
##########?輸出?##############
a?+?b?=?12
print('type?of?a?is?{},?type?of?b?is?{}'.format(type(a),?type(b)))
##########?輸出?##############
type?of?a?is?<class?'str'>,?type?of?b?is?<class?'str'>print('a?+?b?=?{}'.format(int(a)?+?int(b)))
##########?輸出?##############a?+?b?=?3

文件輸入和輸出

生產級別的 Python 代碼,大部分 I/O 則來自于文件

這里有個in.text,完成worldcount功能。

Mr.?Johnson?had?never?been?up?in?an?aerophane?before?and?he?had?read?a?lot?about?air?accidents,?so?one?day?when?a?friend?offered?to?take?him?for?a?ride?in?his?own?small?phane,?Mr.?Johnson?was?very?worried?about?accepting.?Finally,?however,?his?friend?persuaded?him?that?it?was?very?safe,?and?Mr.?Johnson?boarded?the?plane.

His?friend?started?the?engine?and?began?to?taxi?onto?the?runway?of?the?airport.?Mr.?Johnson?had?heard?that?the?most?dangerous?part?of?a?flight?were?the?take-off?and?the?landing,?so?he?was?extremely?frightened?and?closed?his?eyes.

After?a?minute?or?two?he?opened?them?again,?looked?out?of?the?window?of?the?plane,?and?said to his friend。

"Look?at?those?people?down?there.?They?look?as?small?as?ants,?don't?they?"

"Those?are?ants,"?answered?his?friend.?"We're?still?on?the?ground."

現在

  • 讀取文件
  • 去掉所有標點和換行符,將大寫變為小寫
  • 合并相同的詞,統計每個詞出現的頻率,將詞頻從大到小排序
  • 將結果按行輸出文件out.txt
import?re

#?你不用太關心這個函數
def?parse(text):
????#?使用正則表達式去除標點符號和換行符
????text?=?re.sub(r'[^\w?]',?'',?text)

????#?轉為小寫
????text?=?text.lower()
????
????#?生成所有單詞的列表
????word_list?=?text.split('?')
????
????#?去除空白單詞
????word_list?=?filter(None,?word_list)
????
????#?生成單詞和詞頻的字典
????word_cnt?=?{}
????for?word?in?word_list:
????????if?word?not?in?word_cnt:
????????????word_cnt[word]?=?0
????????word_cnt[word]?+=?1
????
????#?按照詞頻排序
????sorted_word_cnt?=?sorted(word_cnt.items(),?key=lambda?kv:?kv[1],?reverse=True)
????
????return?sorted_word_cnt

with?open('in.txt',?'r')?as?fin:
????text?=?fin.read()

word_and_freq?=?parse(text)

with?open('out.txt',?'w')?as?fout:
????for?word,?freq?in?word_and_freq:
????????fout.write('{}?{}\n'.format(word,?freq))

##########?輸出?(省略較長的中間結果)?##########



但是有個問題,如果文件非常的大容易造成內存奔潰

這個時候給 read 指定參數 size,還可以通過 readline() 函數,每次讀取一行。

json文件讀取

import?json

params?=?{
????'symbol':?'123456',
????'type':?'limit',
????'price':?123.4,
????'amount':?23
}

params_str?=?json.dumps(params)

print('after?json?serialization')
print('type?of?params_str?=?{},?params_str?=?{}'.format(type(params_str),?params))

original_params?=?json.loads(params_str)

print('after?json?deserialization')
print('type?of?original_params?=?{},?original_params?=?{}'.format(type(original_params),?original_params))

##########?輸出?##########

after?json?serialization
type?of?params_str?=?<class?'str'>,?params_str?=?{'symbol':?'123456',?'type':?'limit',?'price':?123.4,?'amount':?23}
after?json?deserialization
type?of?original_params?=?<class?'dict'>,?original_params?=?{'symbol':?'123456',?'type':?'limit',?'price':?123.4,?'amount':?23}

json.dumps() 這個函數,接受 Python 的基本數據類型 字典,然后轉化string (json的字符串)

json.loads() 這個函數,接受一個合法字符串(json),然后 轉化為字典

「json 的讀入」

import?json

params?=?{
????'symbol':?'123456',
????'type':?'limit',
????'price':?123.4,
????'amount':?23
}

with?open('params.json',?'w')?as?fout:
????params_str?=?json.dump(params,?fout)

with?open('params.json',?'r')?as?fin:
????original_params?=?json.load(fin)

print('after?json?deserialization')
print('type?of?original_params?=?{},?original_params?=?{}'.format(type(original_params),?original_params))

##########?輸出?##########

after?json?deserialization
type?of?original_params?=?<class?'dict'>,?original_params?=?{'symbol':?'123456',?'type':?'limit',?'price':?123.4,?'amount':?23}

參考:https://time.geekbang.org/column/article/96570

-----------------------公眾號:Python知識圈博客:www.pyzhishiquan.com知乎:Python知識圈微信視頻號:菜鳥程序員 (分享有趣的編程技巧、Python技巧)bilibili:菜鳥程序員的日常(目前原創視頻:22,累計播放量:85萬)

我的微信視頻號定時更新中,近期真人出鏡分析講解 Python 經典習題,后續會分享更多的干貨,歡迎關注我的微信視頻號。

Python知識圈公眾號的交流群已經建立,群里可以領取 Python 相關學習資料,大家可以一起學習交流,效率更高,如果是想發推文、廣告、砍價小程序的敬請繞道!一定記得備注「交流學習」,不然不會通過好友。

掃碼添加,備注:交流學習

往期推薦01

公眾號所有文章匯總導航(2-10更新)

02

Python100例(附PDF下載地址)

03

打基礎一定要吃透這12類 Python 內置函數

↓點擊閱讀原文查看pk哥原創視頻

我就知道你“在看”

總結

以上是生活随笔為你收集整理的python 去除字符串的标点符号 用_Python输入和输出的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。