Ⅰ 请问python中这个为什么能够替换“xxx”呢
strings=['abc','xxx','ksl','xxx']
index=0
for string in strings:
if 'xxx' in string:
strings[index]='[censored]'
index=index+1
print(strings)
源代码(注意源代码的缩进)
这个程序能够替换"xxx"的根本原因是变量index的自增和for循环的取列表strings中的元素string是同步的,
再加上if语句的判断,所以能够准确的替换"xxx",而不会将列表strings中的元素全部替换.
(你可能将if语句中的string字符串变量看成了strings列表了,所以不对了)
这个程序的执行过程是
index==0,for循环从strings列表取出第一个元素'abc',string=='abc',if 'xxx' in string为假,index=index+1
index==1,for循环从strings列表取出第二个元素'xxx',string=='xxx',if 'xxx' in string为真,strings[1]='[censored]',index=index+1
index==2,for循环从strings列表取出第三个元素'ksl',string=='ksl',if 'xxx' in string为假,index=index+1
index==3,for循环从strings列表取出第四个元素'xxx',string=='xxx',if 'xxx' in string为真,strings[3]='[censored]',index=index+1
string列表遍历完了,退出for循环,打印更改后的列表(只有列表的第2,4项被更改)
所以是上述运行结果
Ⅱ 如何使用shell或python进行变量替换
echo $test
这是PHP吧?你确定要用python处理?
思路:
读取a文件内容 遍历每行内容
以单引号分割字符串 得到设置的值
按照b文件格式拼接出c文件的内容
最后生成或覆盖c文件
Ⅲ python 字符串替换 $
试试看将替换后生成的字符串再生成模板后再次替换
tmpl=string.Template(...)
string.Template(tmpl.safe_substitute(**kwg)).safe_substitute(**kwg)
#!/usr/bin/python
#encoding:utf-8
#
#filename:stringTemplateLearning.py
#author:TimWang
#date:June,2013
importstring
tmpl=string.Template("""
$app:
home:$home
data:$data
""")
kwgs=dict(
app="App_Proj",
home="/home/tim/project/$app",
data="$home/data",
)
context=tmpl.safe_substitute(kwgs)
while"$"incontext:
tmpl=string.Template(context)
context=tmpl.safe_substitute(kwgs)
printcontext
>python -u "stringTemplateLearning.py"
App_Proj:
home: /home/tim/project/App_Proj
data: /home/tim/project/App_Proj/data
>Exit code: 0 Time: 0.044
Ⅳ python如何替换指定位置的数据
python可以使用replace方法替换指定字符,根据关键字替换字符串内的所有内容。也可以自定义方法,用循环进行遍历处理
Ⅳ 怎样通过参数替换python脚本里的变量值
一般情况下都是通过import脚本,然后直接调用脚本里的函数,调用函数就可以直接传递参数;因为Python并不像C语言那样有main函数。
import B(脚本名称)
B.hello(参数A,参数B)
Ⅵ python中的变量替换怎么使用
1. 使用连接符: +
world = "World"print "Hello " + world + " ! "
2. 使用占位符来内插
world = "World"print "Hello %s !" % world
3. 使用函数
li = ['my','name','is','bob']mystr = ' '.join(li)print mystr
上面的语句中字符串是作为参数传入的,可以直接用变量替换:
begin_date = '2012-04-06 00:00:00'end_date = '2012-04-06 23:59:59'select * from usb where time between to_date(begin_date,'YYYY-MM-DD HH24:MI:SS') and to_date(end_date,'YYYY-MM-DD HH24:MI:SS')
Ⅶ Python 怎样用变量替换字符串
很简单
只要把
Xpath.replace(Xpath[35:36],num1)
改成
Xpath.replace(Xpath[35:36],str(num1))
以下是replace方法的详细说明
replace(...)
S.replace(old,new[,count])->string
oldreplacedbynew.Iftheoptionalargumentcountis
given,.
参数只能是str类型
Ⅷ 如何用Python来进行查询和替换一个文本字符串
1、说明
可以使用find或者index来查询字符串,可以使用replace函数来替换字符串。
2、示例
1)查询
>>> 'abcdefg'.find('cde')
结果为2
'abcdefg'.find('acde')
结果为-1
'abcdefg'.index('cde')
结果为2
2)替换
'abcdefg'.replace('abc','cde')
结果为'cdedefg'
3、函数说明
1)find(...)
S.find(sub[, start[, end]]) -> int
返回S中找到substring sub的最低索引,使得sub包含在S [start:end]中。 可选的 参数start和end解释为切片表示法。
失败时返回-1。
2)index(...)
S.index(sub[, start[, end]]) -> int
与find函数类似,但是当未找到子字符串时引发ValueError。
3)replace(...)
S.replace(old, new[, count]) -> str
返回S的所有出现的子串的副本旧换新。 如果可选参数计数为给定,只有第一个计数出现被替换。
Ⅸ 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
Ⅹ python,怎么替换丢有变量的信息
# -*- coding: cp936 -*-
import re
p = re.compile(r'<a href="\?stypes=通用名\s*&key=.*">')
s = '<a href="?stypes=通用名&key=妥洛特罗贴剂">妥洛特罗贴剂'
print p.sub('', s)
# >>>
# 妥洛特罗贴剂