day2.py

由于Python源代码也是一个文本文件,所以,当你的源代码中包含中文的时候,在保存源代码时,就需要务必指定保存为UTF-8编码。当Python解释器读取源代码时,为了让它按UTF-8编码读取,我们通常在文件开头写上这两行:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

格式化输出:

最后一个常见的问题是如何输出格式化的字符串。我们经常会输出类似'亲爱的xxx你好!你xx月的话费是xx,余额是xx'之类的字符串,而xxx的内容都是根据变量变化的,所以,需要一种简便的格式化字符串的方式。

 

在Python中,采用的格式化方式和C语言是一致的,用%实现,举例如下:

>>> 'Hello, %s' % 'world'
'Hello, world'
>>> 'Hi, %s, you have $%d.' % ('Michael', 1000000)
'Hi, Michael, you have $1000000.'

左对齐和右对齐知识: 

分别是python和C语言中的不同输出形式,大同小异。

print('%2d-%02d'%(3,1))    #pyhton
print("\%10d,%-10d",652626,653636)    //c

 左对齐即输出向左对齐,如果位数不够右边填充空格print('%-10d'%653636);右对齐同理。

若输出中有%符号:需要利用%转义

>>> 'growth rate: %d %%' % 7
'growth rate: 7 %'

LIST:有append,insert,pop,del等操作

TUPLE:另一类型得list,元素一旦定义就不可以更改,要定义一个只有1个元素的tuple,若如下定义,输出如下,因为()有歧义,会被认为是数学计算中得(),所以应该在括号中添加一个“,”定义为:t=(1,)

t=(1)
>>>t
1

**元组所谓的不可变只是每个元素得指向不变,例如二维元组中存在一个list,则可以修改list中的元素

>>> t = ('a', 'b', ['A', 'B'])
>>> t[2][0] = 'X'
>>> t[2][1] = 'Y'
>>> t
('a', 'b', ['X', 'Y'])

元组得指向

PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py PS C:\Users\29735\Desktop\D创作> & D:/python课程/python2/python.exe c:/Users/29735/Desktop/D创作/day01.py Traceback (most recent call last): File "D:\python课程\python2\Lib\site-packages\urllib3\response.py", line 748, in _error_catcher yield File "D:\python课程\python2\Lib\site-packages\urllib3\response.py", line 1209, in read_chunked chunk = self._handle_chunk(amt) ^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python课程\python2\Lib\site-packages\urllib3\response.py", line 1146, in _handle_chunk value = self._fp._safe_read(amt) # type: ignore[union-attr] ^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python课程\python2\Lib\http\client.py", line 633, in _safe_read raise IncompleteRead(data, amt-len(data)) http.client.IncompleteRead: IncompleteRead(1447 bytes read, 8793 more expected) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\python课程\python2\Lib\site-packages\requests\models.py", line 820, in generate yield from self.raw.stream(chunk_size, decode_content=True) File "D:\python课程\python2\Lib\site-packages\urllib3\response.py", line 1057, in stream yield from self.read_chunked(amt, decode_content=decode_content) File "D:\python课程\python2\Lib\site-packages\urllib3\response.py", line 1189, in read_chunked with self._error_catcher(): File "D:\python课程\python2\Lib\contextlib.py", line 155, in __exit__ self.gen.throw(typ, value, traceback) File "D:\python课程\python2\Lib\site-packages\urllib3\response.py", line 775, in _error_catcher raise ProtocolError(f"Connection broken: {e!r}", e) from e urllib3.exceptions.ProtocolError: ('Connection broken: IncompleteRead(1447 bytes read, 8793 more expected)', IncompleteRead(1447 bytes read, 8793 more expected)) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "c:\Users\29735\Desktop\D创作\day01.py", line 19, in <module> res=requests.get(url=url_detail,headers=headers) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python课程\python2\Lib\site-packages\requests\api.py", line 73, in get return request("get", url, params=params, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python课程\python2\Lib\site-packages\requests\api.py", line 59, in request return session.request(method=method, url=url, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python课程\python2\Lib\site-packages\requests\sessions.py", line 589, in request resp = self.send(prep, **send_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python课程\python2\Lib\site-packages\requests\sessions.py", line 746, in send r.content File "D:\python课程\python2\Lib\site-packages\requests\models.py", line 902, in content self._content = b"".join(self.iter_content(CONTENT_CHUNK_SIZE)) or b"" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python课程\python2\Lib\site-packages\requests\models.py", line 822, in generate raise ChunkedEncodingError(e) requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(1447 bytes read, 8793 more expected)', IncompleteRead(1447 bytes read, 8793 more expected)) PS C:\Users\29735\Desktop\D创作>
最新发布
06-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值