{"id":77,"date":"2021-10-01T00:30:13","date_gmt":"2021-09-30T16:30:13","guid":{"rendered":"https:\/\/www.tysql.com\/?p=77"},"modified":"2021-10-01T00:30:13","modified_gmt":"2021-09-30T16:30:13","slug":"python%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.tysql.com\/index.php\/2021\/10\/01\/python%e5%87%bd%e6%95%b0\/","title":{"rendered":"Python\u51fd\u6570"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:false,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;darcula&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\"># \u5217\u8868\n# \u67e5\u627e\u5143\u7d20\u5728\u5217\u8868\u4e2d\u4f4d\u7f6e\np=list.index(value) # list\u4e3a\u5217\u8868\u7684\u540d\u5b57 value\u4e3a\u67e5\u627e\u7684\u503c p\u4e3avalue\u5728list\u7684\u4f4d\u7f6e\n\n# Python\u4e2dbytes\u4e0e\u5b57\u7b26\u4e32\u7684\u76f8\u4e92\u8f6c\u5316\n# bytes\u8f6c\u5b57\u7b26\u4e32\u65b9\u5f0f\u4e00\nb=b'\\xe9\\x80\\x86\\xe7\\x81\\xab'\nstring=str(b,'utf-8')\nprint(string)\n\n# bytes\u8f6c\u5b57\u7b26\u4e32\u65b9\u5f0f\u4e8c\nb=b'\\xe9\\x80\\x86\\xe7\\x81\\xab'\nstring=b.decode() # \u7b2c\u4e00\u53c2\u6570\u9ed8\u8ba4utf8\uff0c\u7b2c\u4e8c\u53c2\u6570\u9ed8\u8ba4strict\nprint(string)\n\n# bytes\u8f6c\u5b57\u7b26\u4e32\u65b9\u5f0f\u4e09\nb=b'\\xe9\\x80\\x86\\xe7\\x81haha\\xab'\nstring=b.decode('utf-8','ignore') # \u5ffd\u7565\u975e\u6cd5\u5b57\u7b26\uff0c\u7528strict\u4f1a\u629b\u51fa\u5f02\u5e38\nprint(string)\n\n# bytes\u8f6c\u5b57\u7b26\u4e32\u65b9\u5f0f\u56db\nb=b'\\xe9\\x80\\x86\\xe7\\x81haha\\xab'\nstring=b.decode('utf-8','replace') # \u7528\uff1f\u53d6\u4ee3\u975e\u6cd5\u5b57\u7b26\nprint(string)\n\n# \u5b57\u7b26\u4e32\u8f6cbytes\u65b9\u5f0f\u4e00\nstr1='\u9006\u706b'\nb=bytes(str1, encoding='utf-8')\nprint(b)\n\n# \u5b57\u7b26\u4e32\u8f6cbytes\u65b9\u5f0f\u4e8c\nb=str1.encode('utf-8')\nprint(b)\n\n# python3 \u5b57\u5178\u3001\u5217\u8868\u3001\u6570\u7ec4\u548c\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u76f8\u4e92\u8f6c\u5316\n# \u5b57\u5178\ndict_1 = {'1': 'a', '2': 'b', '3': 'c'}\nprint(dict_1)  # \u6253\u5370\u5b57\u5178\nprint(type(dict_1))  # \u6253\u5370\u65b0\u8d4b\u503c\u7684\u5b57\u5178\u7684\u7c7b\u578b\n\nprint(str(dict_1), end='\u5b57\u7b26\u4e32\\n')  # \u628a\u5b57\u5178\u8f6c\u5316\u6210\u5b57\u7b26\u4e32\u540e\u6253\u5370\n\nprint(tuple(dict_1), end='\u5143\u7ec4\\n')  # \u628a\u5b57\u5178\u8f6c\u5316\u4e3a\u5143\u7ec4\u540e\u6253\u5370\n\nprint(list(dict_1), end='\u5217\u8868\\n')  # \u628a\u5b57\u5178\u8f6c\u5316\u4e3a\u5217\u8868\u540e\u6253\u5370\n\n# \u5143\u7ec4\ntuple_1 = (1, 2, 3, 4, 5)\nprint(tuple_1)  # \u6253\u5370\u5143\u7ec4\nprint(type(tuple_1))  # \u6253\u5370\u65b0\u8d4b\u503c\u7684\u5143\u7956\u7684\u7c7b\u578b\n\nprint(str(tuple_1), end='\u5b57\u7b26\u4e32\\n')  # \u628a\u5143\u7ec4\u8f6c\u5316\u6210\u5b57\u7b26\u4e32\u540e\u6253\u5370\n\nprint(list(tuple_1), end='\u5217\u8868\\n')  # \u628a\u5143\u7ec4\u8f6c\u5316\u4e3a\u5217\u8868\u540e\u6253\u5370\n\n# \u5143\u7ec4\u4e0d\u53ef\u8f6c\u5316\u4e3a\u5b57\u5178\n\n#\u5217\u8868\nlist_1 = [1, 2, 3, 4, 5]\nprint(list_1)  # \u6253\u5370\u5217\u8868\nprint(type(list_1))  # \u6253\u5370\u65b0\u8d4b\u503c\u7684\u5217\u8868\u7684\u7c7b\u578b\n\nprint(str(list_1), end='\u5b57\u7b26\u4e32\\n')  # \u628a\u5217\u8868\u8f6c\u5316\u6210\u5b57\u7b26\u4e32\u540e\u6253\u5370\n\nprint(tuple(list_1), end='\u5143\u7ec4\\n')  # \u628a\u5217\u8868\u8f6c\u5316\u4e3a\u5143\u7ec4\u540e\u6253\u5370\n\n#\u5b57\u7b26\u4e32\nstring_1 = '(1, 2, 3, 4, 5)'\nprint(string_1)  # \u6253\u5370\u5b57\u7b26\u4e32\nprint(type(string_1))  # \u6253\u5370\u65b0\u8d4b\u503c\u7684\u5b57\u7b26\u4e32\n\nprint(tuple(eval(string_1)), end='\u5143\u7ec4\\n')  # \u628a\u5b57\u7b26\u4e32\u8f6c\u5316\u6210\u5143\u7ec4\u540e\u8f93\u51fa\n\nprint(list(eval(string_1)), end='\u5217\u8868\\n')  # \u628a\u5b57\u7b26\u4e32\u8f6c\u5316\u6210\u5217\u8868\u540e\u8f93\u51fa\n\n# \u53ea\u6709\u7279\u6b8a\u5b57\u7b26\u4e32\u624d\u80fd\u8f6c\u5316\u4e3a\u5b57\u5178\nstring_2 = '{&quot;name&quot;:&quot;tom&quot;,&quot;age&quot;:6}'\nprint(string_2)  # \u6253\u5370\u65b0\u8d4b\u503c\u7684\u5b57\u7b26\u4e32\nprint(type(string_2))  # \u6253\u5370\u65b0\u8d4b\u503c\u7684\u5b57\u7b26\u4e32\u7684\u7c7b\u578b\n\nprint(eval(string_2), end='\u5b57\u5178\\n')  # \u628a\u5b57\u7b26\u4e32\u8f6c\u5316\u4e3a\u5b57\u5178\u540e\u6253\u5370\n\n\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":3,"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":80,"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/posts\/77\/revisions\/80"}],"wp:attachment":[{"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}