网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何用python输出和输入文件及信息-创新互联

小编给大家分享一下如何用python输出和输入文件及信息,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

创新互联公司是一家专注于成都网站制作、网站建设与策划设计,平昌网站建设哪家好?创新互联公司做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:平昌等地区。平昌做网站价格咨询:18980820575

利用语句有: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 , type of b is 
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输出和输入文件及信息有了一定的了解,想了解更多相关知识,欢迎关注创新互联-成都网站建设公司行业资讯频道,感谢各位的阅读!


名称栏目:如何用python输出和输入文件及信息-创新互联
路径分享:http://bjjierui.cn/article/jgghi.html

其他资讯