shengnanonly 2020-06-03
#!/usr/local/bin/python3.7 """ @File : str_replace.py @Time : 2020/06/03 @Author : Mozili """# 输入多行字符串 data = [] str = input(‘输入要调整的字符串:\n‘) data.append(str) stopword = ‘‘#停止条件 for line in iter(input,stopword):#iter()中第一个参数是可调用的,即可以像函数一样调用他,因此是input,而不是input() data.append(line.split()) # print(data) # 遍历列表 for i in range(len(data)): if ‘:‘ in data[0]: if i==0: s0 = data[0].split(‘:‘)[0] s1 = data[0].split(‘:‘)[1].strip(‘ ‘) # 给字符串加单引号 s0 = ‘{}{}{}‘.format("‘", s0, "‘") s1 = ‘{}{}{}‘.format("‘", s1, "‘") # 拼接成一个新的字符串 new_str = ‘data = ‘ + ‘{‘ + ‘\n ‘ + s0 + ‘:‘ + s1 + ‘,‘ else: s0 = data[i][0].split(":")[0] s1 = data[i][1] s0 = ‘{}{}{}‘.format("‘", s0, "‘") s1 = ‘{}{}{}‘.format("‘", s1, "‘") if i==len(data)-1: new_str = ‘ ‘ + s0 + ‘:‘ + s1 + ‘\n‘ + ‘}‘ else: new_str = ‘ ‘ + s0 + ‘:‘ + s1 + ‘,‘ elif ‘&‘ in data[0]: # 以&分割字符串 s_list = data[0].split(‘&‘) # print(s_list) for n in range(len(s_list)): # 将字符串中的=换成: str = s_list[n].replace(‘=‘, ‘:‘) # print(str) # 把字符串转为list str_list = list(str) # print(str_list) # 确认:在列表中的索引值 i = str_list.index(‘:‘) # print(i) # 在:的前面添加‘ str_list.insert(i, "‘") # 在:的后面添加‘ str_list.insert(i+2, "‘") # 将list转换为str str = ‘‘.join(str_list) # 给字符串添加单引号 str = ‘{}{}{}‘.format("‘", str, "‘") if n==0: new_str = ‘data = ‘ + ‘{‘ + ‘\n ‘ + str + ‘,‘ else: if n==len(s_list)-1: new_str = ‘ ‘ + str + ‘}‘ else: new_str = ‘ ‘ + str + ‘,‘ print(new_str) else: i=0 while i < len(data): # print(i) if i==0: s = data[0].split(‘ ‘) # 获取第一个字符串 s0 = s[0] # 给字符串加单引号 s0 = ‘{}{}{}‘.format("‘", s0, "‘") # 获取第二个字符串 s1 = s[1] s1 = ‘{}{}{}‘.format("‘", s1, "‘") new_str = ‘data = ‘ + ‘{‘ + ‘\n ‘ + s0 + ‘:‘ + s1 + ‘,‘ else: # 判断列表的长度,处理没有值的键值对 if len(data[i])==1: s = data[i][0] s0 = ‘{}{}{}‘.format("‘", s, "‘") new_str = ‘ ‘ + s0 + ‘:‘ + "‘‘" + ‘,‘ else: s0 = data[i][0] s1 = data[i][1] # 给字符串添加单引号 s0 = ‘{}{}{}‘.format("‘", s0, "‘") s1 = ‘{}{}{}‘.format("‘", s1, "‘") # 判断是否是最后一组数据 if i == len(data)-1: new_str = ‘ ‘ + s0 + ‘:‘ + s1 + ‘}‘ else: new_str = ‘ ‘ + s0 + ‘:‘ + s1 + ‘,‘ i+=1 print(new_str) break print(new_str) # 支持替换以下三种格式的字符串 str1 = """ email 18827441xxxx icode origURL http://www.renren.com/home domain renren.com key_id 1 captcha_type web_login password 32904758c29a901f87064f3aa151f9c373112ed66370fe9b28152f0ce63d3796 rkey d6b3acc434f19c92fad1f33176e506f0 f https%3A%2F%2Fbaidu.com%2F """ str2 = """ from: en to: zh-Hans text: cat """ str3 = "ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=python中正则替换"
>>> a = " a b c ". # replace主要用于字符串的替换replace. # join为字符字符串合成传入一个字符串列表,split用于字符串分割可以按规则进行分割。>>> c =