「@Author:By Runsen」

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

input

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

###### 输入 ######your name:Runsenyou 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()1b = input()2

print('a + b = {}'.format(a + b))########## 输出 ##############a + b = 12print('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 serializationtype of params_str = <class 'str'>, params_str = {'symbol': '123456', 'type': 'limit', 'price': 123.4, 'amount': 23}after json deserializationtype 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 deserializationtype of original_params = <class 'dict'>, original_params = {'symbol': '123456', 'type': 'limit', 'price': 123.4, 'amount': 23}

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

本文已收录 GitHub,传送门~[1] ,里面更有大厂面试完整考点,欢迎 Star。

Reference

[1]

传送门~: https://github.com/MaoliRUNsen/runsenlearnpy100

今天的文章到这里就结束了,如果喜欢本文的话,请来一波素质三连,给我一点支持吧(关注、在看、点赞)。

更多的文章

点击下面小程序

- END -

python中倒着输出输入值_十五、深入Python输入和输出相关推荐

  1. python中cgi到底是什么_十分钟搞懂什么是CGI(转)

    原文:CGI Made Really Easy,在翻译的过程中,我增加了一些我在学习过程中找到的更合适的资料,和自己的一些理解.不能算是严格的翻译文章,应该算是我的看这篇文章的过程的随笔吧. CGI真 ...

  2. 如何解决python中编码错误的问题_【总结】Python 2.x中常见字符编码和解码方面的错误及其解决办法...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 对于Python解析器 而Python解析器所干的事情,就是: Python解析器,根据当前的所用的字符串编码类型 此字符串编码类型,是你自己所设置的 不 ...

  3. python中的画布背景设置_教你用python画图—Turtle详细教程

    Turtle模块绝对是吸引非专业代码开发者人员学习python入门的好工具 通过turtle几行代码的执行软件就会画出漂亮的图形,美观而且有成就感,这样一下子对python编程就产生了兴趣. 这些漂亮 ...

  4. python中ret是什么意思_数据结构图在python中的应用

    原标题:数据结构图在python中的应用 程序世界里,有很多的数据结构,比如:堆.栈.链表等等,今天要讲的就是图数据结构啦. 相信大家都使用过或者听说过图数据库吧,我们就来看看最简单的图数据结构算法. ...

  5. python中的声音处理文件库_介绍几个python的音频处理库

    图 3比如我的机器的地址就是 192.168.152.130.然后将这个ip地址填入 图2 的Host Name 一栏,注意默认端口为22,不要去改它,然后点击open,就会弹出一个登陆界面,接着输入 ...

  6. python中换行符怎么写_语法 - 如何在Python中执行换行符(换行符)?

    语法 - 如何在Python中执行换行符(换行符)? 我有一长串代码,我希望在多行之间分解. 我使用什么,语法是什么? 例如,添加一串字符串, e = 'a' + 'b' + 'c' + 'd' 并将 ...

  7. python中声明变量注意事项_我们如何在Python中声明变量?

    简短的答案是,无需在Python中声明变量. 以下是更详细的描述. 静态类型语言(C,C ++,Java,C#)要求在程序中使用变量之前,必须先声明要使用的变量的名称和类型声明.相应的语言编译器确保将 ...

  8. python 中split函数的应用_举例详解Python中的split()函数的使用方法

    函数:split() python中有split()和os.path.split()两个函数,具体作用如下: split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(li ...

  9. python中一个范围怎么表示_我应该如何处理Python中的包含范围?

    我在一个领域工作,在这个领域中,范围通常被包括在内地描述.我有人类可读的描述,例如from A to B,它表示包含两个端点的范围,例如from 2 to 4表示2, 3, 4. 在Python代码中 ...

最新文章

  1. Foxmail6密码获取案例
  2. 小卡片遇热就变机器人,不插电就能运动,哈佛加州理工新研究登上Nature子刊...
  3. androidstudio build tools安装_如何导入Android Studio(AS)项目
  4. 图神经网络三剑客:GCN、GAT与GraphSAGE
  5. Python学习之路:函数介绍
  6. 外贸常用术语_外贸中常用的会计术语及付款方式术语 | 会计英语
  7. 【redis】redisDesktopManager之redis可视化客户端 界面介绍
  8. android 好看的计算器,从未见过如此丑的计算器 – 计算管家 #Android
  9. mac 安装homebrew 并替换清华镜像
  10. r语言 linux使用教程,R语言初级教程: R编程环境的搭建
  11. VBScript详解(一)
  12. 基于FPGA的数字时钟
  13. 06-13最新Xcode7 beta版迅雷离线下载
  14. Arduino平衡小车
  15. Java程序员面试需要注意什么?
  16. Swift 与OC转换
  17. 20210318 东南大学电气工程学院毕业分布--可视化地图
  18. 通用知识图谱VS行业知识图谱
  19. 智能卡脚本语言easyCard
  20. 该产品与此版 VMware Workstation 不兼容,因此无法使用

热门文章

  1. docker二进制安装mysql_Docker搭建MySQL读写分离主从模式 分布式数据库中间件Mycat分库分表应用...
  2. 应用层为何不能设置分辨率
  3. C++远征之封装篇——对象数组,对象成员
  4. python 编程模型
  5. 十六进制转化为十进制
  6. 高版本号chrome安装flashplayer debuger后无法使用的问题
  7. CodeForces - 796D Police Stations bfs
  8. 图解,C语言希尔排序
  9. Linus Torvalds:我们都老了,但Linux维护者真的很难找
  10. C 实现 删除字符串空白符的函数 strtrim