日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:怎么用python輸出和輸入文件及信息?

所有代碼程序由自己編寫后,總歸是為了服務(wù)用戶,因?yàn)樵诹私獾叫畔r(shí),使用編程軟件,要去自動化處理這些內(nèi)容,怎么做呢?一起來看下吧~

利用語句有:input和print語句

關(guān)于Input代碼演示:

name = input('your name:')
gender = input('you are a boy?(y/n)')
 
###### 輸入 ######
your name:Jack
you are a boy?
 
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. Jack.

input函數(shù)暫停運(yùn)行,等待鍵盤輸入,直到按下回車,輸入的類型永遠(yuǎn)是字符串

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 , type of b is 
print('a + b = {}'.format(int(a) + int(b)))
########## 輸出 ##############
a + b = 3

文件輸入和輸出

生產(chǎn)級別的 Python 代碼,大部分 I/O 則來自于文件,這里有個in.text:

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."

現(xiàn)在讀取文件:

  • 去掉所有標(biāo)點(diǎn)和換行符,將大寫變?yōu)樾?/p>

  • 合并相同的詞,統(tǒng)計(jì)每個詞出現(xiàn)的頻率,將詞頻從大到小排序

  • 將結(jié)果按行輸出文件out.txt

import re
 
# 你不用太關(guān)心這個函數(shù)
def parse(text):
    # 使用正則表達(dá)式去除標(biāo)點(diǎn)符號和換行符
    text = re.sub(r'[^\w ]', '', text)
 
    # 轉(zhuǎn)為小寫
    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))
 
########## 輸出 (省略較長的中間結(jié)果) ##########

大家也可以根據(jù)上面代碼教學(xué),套用嘗試下哦~如需更多python實(shí)用知識,點(diǎn)擊進(jìn)入PyThon學(xué)習(xí)網(wǎng)教學(xué)中心。


本文標(biāo)題:創(chuàng)新互聯(lián)Python教程:怎么用python輸出和輸入文件及信息?
網(wǎng)頁URL:http://m.5511xx.com/article/codispi.html