diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_Super_Coding/week2/content.txt" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_Super_Coding/week2/content.txt" new file mode 100644 index 0000000000000000000000000000000000000000..a9447d37205c5872093f64f559329390df18839e --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_Super_Coding/week2/content.txt" @@ -0,0 +1,17 @@ + + def str_format(self): + # 字符串格式化 + str_format = '格式化' + format_result = 'format方法{}'.format(str_format) + + index_format = 'index format {0}'.format(str_format) + + args_format = 'args foramt {str_format}'.format(str_format=str_format) + + variable_foramt = f'变量方式{str_format}' + + num = 520.1314326 + num_format = '{:.4f}'.format(num) + + return format_result, index_format, args_format, variable_foramt, num_format + \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_Super_Coding/week2/\347\254\254\344\272\214\345\221\250-\347\254\254\344\272\214\350\212\202-\344\275\234\344\270\232.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_Super_Coding/week2/\347\254\254\344\272\214\345\221\250-\347\254\254\344\272\214\350\212\202-\344\275\234\344\270\232.py" new file mode 100644 index 0000000000000000000000000000000000000000..091addf96a6cdb8d255951180290954d37c54e6e --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_Super_Coding/week2/\347\254\254\344\272\214\345\221\250-\347\254\254\344\272\214\350\212\202-\344\275\234\344\270\232.py" @@ -0,0 +1,97 @@ +class Home_Work(object): + """ + 作业类 + """ + def decode_and_encode(self): + """ + 字符串的编码和解码 + """ + en_string = '字符串' + en_string = en_string.encode('utf-8') # 编码 + de_string = en_string.decode('utf-8') # 解码 + return en_string, de_string + + def str_crud(self): + """ + 字符串crud + """ + create_str = '字符串' + create_str += '创建' + + index_ = create_str[2] # 索引字符 + find_str = create_str.find('创') # 获取目标字符索引值 + + startswith_str = '开始字符' + print(startswith_str.startswith('开')) # True + + endswith = '结束字符' + print(endswith.endswith('符')) # True + + replace_str = '替换&字符' + replace_str = replace_str.replace('&', '') + split_str = '切割*字符' + split_str = split_str.split('*') + + join_str = ','.join(split_str) # 拼接 + + strip_str = ' 删除前后空格 ' + strip_str = strip_str.strip() + + return create_str, find_str, replace_str, split_str, strip_str, index_ + + def str_format(self): + """ + 字符串格式化 + """ + str_format = '格式化' + format_result = 'format方法{}'.format(str_format) + + index_format = 'index format {0}'.format(str_format) + + args_format = 'args foramt {str_format}'.format(str_format=str_format) + + variable_foramt = f'变量方式{str_format}' + + num = 520.1314326 + num_format = '{:.4f}'.format(num) + + return format_result, index_format, args_format, variable_foramt, num_format + + def save_content(self): + """ + 保存文件 + """ + f = open('week2/content.txt', 'w', encoding='utf-8') + content = """ + def str_format(self): + # 字符串格式化 + str_format = '格式化' + format_result = 'format方法{}'.format(str_format) + + index_format = 'index format {0}'.format(str_format) + + args_format = 'args foramt {str_format}'.format(str_format=str_format) + + variable_foramt = f'变量方式{str_format}' + + num = 520.1314326 + num_format = '{:.4f}'.format(num) + + return format_result, index_format, args_format, variable_foramt, num_format + """ + f.write(content) + f.close() + print('写入文件成功!') + + + + + + +if __name__ == "__main__": + hw = Home_Work() + hw.decode_and_encode() + hw.str_format() + hw.str_format() + hw.save_content() + diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_Super_Coding/week2/\347\254\254\344\272\214\345\221\250-\347\254\254\344\272\214\350\212\202-\351\232\217\345\240\202\347\254\224\350\256\260.md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_Super_Coding/week2/\347\254\254\344\272\214\345\221\250-\347\254\254\344\272\214\350\212\202-\351\232\217\345\240\202\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..d084b5c1d5ea1950e3a1a5eaa32f55ee1eb9e971 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/2\347\217\255/2\347\217\255_Super_Coding/week2/\347\254\254\344\272\214\345\221\250-\347\254\254\344\272\214\350\212\202-\351\232\217\345\240\202\347\254\224\350\256\260.md" @@ -0,0 +1,283 @@ +# 第二周-第二节课 + +## 字符串(字符序列)和字节序列 + +- 字符 + + - 由于历史原因, 将字符定义为`unicode`字符还不够准确, 但是未来字符的定义一定是`unicode`字符 + +- 字节 + + 就是字符的二进制表现形式 + +- 码位 + + 我们计算机显示的实际上是码位 + + ``` + >>> '你好'.encode("unicode_escape").decode() + '\\u4f60\\u597d' + >>> + >>> '\u4f60\u597d' + '你好' + ``` + + - `unicode`标准中以4~6个十六进制数字表示 + +- 编码 + + - 字符序列(string) -> 字节序列(bytes) -------------编码(encode) + + ``` + >>> "你好".encode("utf-8") + b'\xe4\xbd\xa0\xe5\xa5\xbd' + ``` + + - 字节序列(bytes) -> 字符序列(string) -------------解码(decode) + + ``` + >>> b + b'\xe4\xbd\xa0\xe5\xa5\xbd' + >>> b.decode("utf") + '你好' + ``` + +- 编码错误 + + - 乱码和混合编码 + + - 检查编码 + + 没有办法通过字节序列来得出编码格式, 都是统计学来预估当前的编码 + + ``` + # 安装chardet + pip install chardet + + # 导入charet + >>> import chardet + >>> chardet.detect(b) + ``` + + - 解决乱码和混合编码 + + - 忽略错误编码 + + ``` + >>> b_2.decode("utf-8", errors='ignore') + '你好' + ``` + + - 利用鬼符来替换 + + ``` + >>> b_2.decode("utf-8", errors='replace') + '你好��' + ``` + + + +## 字符串的CRUD操作 + +``` +通过dir("")可以查看当前字符串的操作方法 +``` + +- Create(创建) + + - `+` + + ``` + >>> a = "a" + >>> id(a) + 22951584 + >>> a = a + "b" + >>> id(a) + 60513280 + >>> a + 'ab' + ``` + + - `+=` + + ``` + a += "b" 就是 a = a + "b" 省略写法 + ``` + +- Retrieve(检索) + + - 根据索引获取字符 + + 在计算机语言当中, 索引值是从0开始数的 + + ``` + >>> a = "hello, world" + >>> a[1] + 'e' + ``` + + - find和index(获取目标字符的索引值) + + ``` + >>> a.find("e") + 1 + >>> a.find("!") + -1 + + # 找不到目标字符时, index会报错 + >>> a.index("!") + Traceback (most recent call last): + File "", line 1, in + ValueError: substring not found + ``` + + - startswith和endswith + + ``` + >>> f = "2020-11-22-xxxxx" + >>> f.startswith("2020-11-22") + True + >>> f = "xxxxx.jpg" + >>> f.endswith("jpg") + True + ``` + +- UPDATE(更新) + + - replace(替换) + + 返回的是一个新的字符串 + + ``` + a.replace("wer", "wor") + ``` + + - split(分割) + + ``` + >>> a = "<>, <>, <>" + >>> a.split(",") + ['<>', ' <>', ' <>'] + ``` + + - join(拼接) + + ``` + >>> b + ['<>', ' <>', ' <>'] + >>> ",".join(b) + '<>, <>, <>' + ``` + +- DELETE(删除) + + - strip + + ``` + >>> a + ' hello, world ' + >>> a.strip() + 'hello, world' + >>> + + ``` + + - lstrip + + - rstrip + +## 字符串的输出和输入 + +- 保存到文件 + + ``` + # open函数打开一个文件, 没有文件会新建, 但是路劲不对会报错 + # 指定文件名, 方法(读, 写, 追加), 编码格式 + output = open("output.txt", "w", encoding="utf-8") + content = "hello, world" + # 正式写入文件 + output.write(content) + # 关闭文件句柄 + output.close() + ``` + +- 读取文件 + + ``` + input = open("output.txt", "r", encoding="utf-8") + # 获取文件中的内容 + content = input.read() + print(content) + + # 暂时理解为只能读取一遍 + content_2 = input.read() + print(content_2) + ``` + +- 追加文件 + + ``` + output = open("output.txt", "a", encoding="utf-8") + content = "\nhello, world" + # 正式写入文件 + output.write(content) + # 关闭文件句柄 + output.close() + ``` + +## 字符串的格式化输出 + +- format + + - 按传入参数默认顺序 + + ``` + a = "ping" + b = "pong" + + "play pingpong: {}, {}".format(a, b) + ``` + + - 按指定参数索引 + + ``` + a = "ping" + b = "pong" + + "play pingpong: {0}, {1}, {0}, {1}".format(a, b) + ``` + + - 按关键词参数 + + ``` + a = "ping" + b = "pong" + + print("play pingpong: {a}, {b}, {a}, {b}".format(a='ping', b='pong')) + ``` + + - 按变量(推荐, 但是只有3.6以上才可以使用) + + ``` + a = "ping" + b = "pong" + + print(f"playing pingpong: {a}, {b}") + ``` + + - 小数的表示 + + ``` + >>> "{:.2f}".format(3.14159) + '3.14' + >>> + ``` + +- % + + ``` + >>> "playing %s %s" % ("ping", "pong") + 'playing ping pong' + + ``` +