当前位置:首页 » 编程语言 » python中字符串的替换字符串

python中字符串的替换字符串

发布时间: 2022-08-24 07:12:26

1. python中文字符串替换字符

a.replace("|","\n")应该改成a=a.replace("|","\n")
因为a.replace()并没有改变a的值,只是将从a读取出来的内容改变了

2. python 字符串替换求解

使用正则,

#!/usr/bin/python
#-*-coding:UTF-8-*-

importre

phone="2004-959-559#这是一个国外电话号码"

#删除字符串中的Python注释
num=re.sub(r'#.*$',"",phone)
print"电话号码是:",num

#删除非数字(-)的字符串
num=re.sub(r'D',"",phone)
print"电话号码是:",num

以上实例执行结果如下:

电话号码是: 2004-959-559

电话号码是 : 2004959559

3. python 中怎么替换字符串

Python替换某个文本中的字符串,然后生成新的文本文档,代码如下:import osos.chdir('D:\\') # 跳到D盘if not os.path.exists('test1.txt'): # 看一下这个文件是否存在exit(-1) #不存在就退出lines = open('test1.txt').readlines() #打开文件,读入每一行fp = open(''test2.txt','w') #打开你要写得文件test2.txtfor s in lines:# replace是替换,write是写入fp.write( s.replace('love','hate').replace('yes','no')) fp.close() # 关闭文件

4. python的字符串替换问题

楼主搞生物的?很像碱基对啊。replace是替换整串字符串的,但是这里不方便,因为你把AA替换成TT后,就变成TTTT,然后再替换,变为AAAA,没有达到效果,除非你用另外的字符代替,不过,这样就没有python的简洁优美了,所以这个问题用re最方便,下面是代码:

#coding=utf-8

importre

astr='AATTCCGG'
charmap={'AA':'TT','TT':'AA','CC':'GG','GG':'CC'}
new=re.sub(r'AA|TT|CC|GG',lambdax:charmap[x.group(0)],astr)
print(new)#python2为printnew

5. python 字符串替换

str='aaaaaaaaaa'
ls=list(str)
ls[2]='0'
ls[3]='0'
ls[4]='0'
ls[5]='0'
ls[6]='0'
new_str=''.join(ls)#'aa00000aaa'

6. python怎么替换很多特定字符串为其他的字符串

  1. 用链式替换,示例如下:

str1='abcdef'
str2=str1.replace('a','1').replace('b','2')
print(str2)
#12cdef

2.用正则替换,示例如下:

importre
str3='abcdef'
str4=re.compile('(a|b)').sub('1',str1)
print(str4)
#11cdef

1 & 2结合应该能解决问题

7. Python正则表达式如何进行字符串替换

Python正则表达式在使用中会经常应用到字符串替换的代码。有很多人都不知道如何解决这个问题源码天空,下面的代码就告诉你其实这个问题无比的简单,希望你有所收获。1.替换所有匹配的子串用newstring替换subject中所有与正则表达式regex匹配的子串result, number = re.subn(regex, newstring, subject) 2.替换所有匹配的子串(使 用正则表达式对象)rereobj = re.compile(regex) result, number = reobj.subn(newstring, subject)字符串拆分 Python字符串拆分reresult = re.split(regex, subject) 字符串拆分(使用正则表示式对象)rereobj = re.compile(regex) result = reobj.split(subject)匹配 下面列出Python正则表达式的几种匹配用法:1.测试正则表达式是否 匹配字符串的全部或部分regex=ur"..." #正则表达式if re.search(regex, subject): do_something() else:do_anotherthing()2.测试正则表达式是否匹配整个字符串regex=ur"...\Z" #正则表达式末尾以\Z结束if re.match(regex, subject): do_something() else: do_anotherthing() 3. 创建一个匹配对象,然后通过该对象获得匹配细节regex=ur"..." #正则表达式match = re.search(regex, subject) if match: # match start: match.start() # match end (exclusive): match.end() # matched text: match.group() do_something() else: do_anotherthing() 以上就是对Python正则表达式在字符串替换中的具体介绍。

8. python的replace函数怎么用

Python replace()方法把字符串中的old(旧字符串)替换成new(新字符串),如果指定三个参数max,则替换不超过max次。

语法

replace()方法语法:

str.replace(old, new[, max])

参数

old -- 将被替换的子字符串;

new -- 新字符串,用于替换old子字符串;

max -- 可选字符串,替换不超过max次。

返回值

返回字符串中的old(旧字符串)替换成new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过max次。

实例

#!/usr/bin/python

str = "this is string example....wow!!! this is really string";

print str.replace("is", "was");

print str.replace("is", "was", 3);

输出结果

thwas was string example....wow!!! thwas was really string

thwas was string example....wow!!! thwas is really string

9. python中如何替换字符串

replace()实现字符串替换

使用案例

10. python-字符串替换

很简单只要把Xpath.replace(Xpath[35:36],?num1)改成Xpath.replace(Xpath[35:36],?str(num1))以下是replace方法的详细说明?replace(...)?????S.replace(old,?new[,?count])?->?string?????Return?a??of?string?S?with?all?occurrences?of?substring?????old?replaced?by?new.??If?the?optional?argument?count?is?????given,?only?the?first?count?occurrences?are?replaced.参数只能是str类型

热点内容
python查看进程 发布:2024-05-19 22:59:37 浏览:158
androidhtml颜色 发布:2024-05-19 22:58:34 浏览:847
米3系统存储和内存设备 发布:2024-05-19 22:50:50 浏览:214
途乐有哪些越野配置 发布:2024-05-19 22:49:53 浏览:673
php检测变量 发布:2024-05-19 22:45:31 浏览:322
结构与算法 发布:2024-05-19 22:32:22 浏览:588
ubuntuphp版本 发布:2024-05-19 21:59:12 浏览:929
解压文案馆 发布:2024-05-19 21:58:54 浏览:871
苏宁访问数 发布:2024-05-19 21:53:49 浏览:581
湿地下载ftp 发布:2024-05-19 21:46:10 浏览:487