国产成人AV无码一二三区,少女1到100集,国产精品久久久久精品综合紧,巜公妇之诱感肉欲HD在线播放

文章 > Python基础教程 > 怎么用python输出和输入文件及信息?

怎么用python输出和输入文件及信息?

头像

2020-10-27 17:14:343727浏览 · 0收藏 · 0评论

所有代码程序由自己编写后,总归是为了服务用户,因为在了解到信息时,使用编程软件,要去自动化处理这些内容,怎么做呢?一起来看下吧~

利用语句有:input和print语句

关于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函数暂停运行,等待键盘输入,直到按下回车,输入的类型永远是字符串

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:

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))
 
########## 输出 (省略较长的中间结果) ##########

大家也可以根据上面代码教学,套用尝试下哦~如需更多python实用知识,点击进入PyThon学习网教学中心

关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

欧美大片免费PPT| 武松叼着潘金莲奶头的是谁扮演的| 《工地夫妻》原声版| 免费观看已满十八岁电视剧高清版| 添女人荫蒂全部过程AV视频| 三亚私人高清影院品牌加盟电话 | 《甜蜜惩罚2》第二季的剧情| 胡乱说深见君| 电影《乳妓》在线观看| 日韩精品国产一区二区三区| 日韩精品一区二区三区| 男人女人免费观看40集电视剧| 东北老阿姨高清免费观看电视剧 | 无人区免费观看高清电视剧| 双胞胎母亲三飞| 适合夫妻晚上看的三张爱情电视剧| B站夫妻进入高峰期需要多久| 双腿无力合拢红肿外翻男生原因| 性做爰1-7伦| 打扑克| 床上108种扦插方法| CANOPEN国产| 东北娘们国语版免费播放| 大叔与妹妹免费观看电视剧高清| 《出差的日子》叶爱| 女儿与爸爸高清免费版电视剧| 男生把困困放到女生困困视频高清 | 口咬的10个绝招图片| 疯狂二人房间荷尔蒙爆发原声| 疯狂祖母免费观看高清版| 赠刘景文| 麻花传剧MV无痕免费观看姐弟 | 菠萝菠萝蜜在线观看免费版电视剧 | 成品网站1688在线应用评价| 爷爷的鸡又黑又长下一句怎么接| 爸爸VICIUSSS幼儿小女孩| 120分钟做爰A片免费| 春夜影院电视剧免费播放| 描写燕子的古诗词| 图书馆挺进她腿间湿润漫画 | 妈妈总是晚上装睡配合孩子