前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 系列文章 —— input_output 篇

Python 系列文章 —— input_output 篇

原创
作者头像
玩转编程
修改2022-01-14 14:50:29
2380
修改2022-01-14 14:50:29
举报
文章被收录于专栏:玩转编程玩转编程
  • input_output
代码语言:python
复制
# 1 str.format() 示例

# 1)
print('{}网址: "{}!"'.format('Python技术', 'www.justdopython.com'))

# 2)
print('{0} 和 {1}'.format('Hello', 'Python'))
print('{0} {1}'.format('Hello', 'Python'))
print('{1} {0}'.format('Hello', 'Python'))

# 3)
print('{name}网址: {site}'.format(name='Python技术', site='www.justdopython.com'))

# 4)
print('电商网站 {0}, {1}, {other}。'.format('淘宝', '京东', other='拼多多'))

# 5)
"repr() shows quotes: {!a}; str() doesn't: {!s}".format('test1', 'test2')

# 6)
import math
print('The value of PI is approximately {0:.3f}.'.format(math.pi))

# 7)
table = {'Sjoerd': 123, 'Jack': 456, 'Dcab': 789}
for name, phone in table.items():print('{0:10} ==> {1:10d}'.format(name, phone))

# 8)
table = {'Sjoerd': 123, 'Jack': 456, 'Dcab': 789789789789}
print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; ' 'Dcab: {0[Dcab]:d}'.format(table))

table = {'Sjoerd': 123, 'Jack': 456, 'Dcab': 789789789789}
print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))


# 2
str = input("请输入:");
print ("输入的内容是: ", str)

# 3
# read()
f = open('tmp.txt', 'r')
str = f.read(5)
print(str)
f.close()

# readline()
f = open('tmp.txt', 'r')
str = f.readline()
print(str)
f.close()

# readlines()
f = open('tmp.txt', 'r')
str = f.readlines(1)
print(str)
f.close()

# write()
f = open('tmp.txt', 'w')
num = f.write('Hello Python')
print(num)
f.close()

# seek()
f = open('tmp.txt', 'rb+')
f.write(b'0123456789abcdef')
# 移动到文件的第 6 个字节
f.seek(5)
print(f.read())

# tell()
f = open('tmp.txt', 'r')
f.seek(5)
print(f.tell())

# close()
with open('tmp.txt', 'r') as f:
     read_data = f.read()
print(f.closed)

# with 使用
with open('tmp.txt', 'r') as f:
    read_data = f.read()
print(f.closed)

# json 操作
import json
data = {'id':'1', 'name':'jhon', 'age':12}
with open('t.json', 'w') as f:
   json.dump(data, f)
with open("t.json", 'r') as f:
   d = json.load( f)
print(d)

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档