如何使用PHP preg_replace函数制作正则表达式模式,删除所有不适合某种模式的字符.例如:
[a-zA-Z0-9]
Run Code Online (Sandbox Code Playgroud) 我想要匹配这个:
[8]\w+-[22]\w+
import ctypes
a = 'abc'
b = ctypes.string_at(id(a), 3)
c = ctypes.string_at(id(a) + 20, 3)
Run Code Online (Sandbox Code Playgroud)
我希望b的结果是'abc',但事实并非如此; 并且c的结果是'abc'.我不知道为什么.有谁能解释我?
def file_open(filename):
fo=open(filename,'r')
#fo.seek(5)
fo.read(3)
fo.close()
file_open("file_ro.py")
Run Code Online (Sandbox Code Playgroud)
我希望上面的程序从文件返回前3个字节.但它什么也没有回报.当我在交互式python命令提示符中运行它们时 - 我得到预期的输出!
我试图访问这个PHP数组没有运气,我想访问[icon] => icon.png
Array ( [total] => 2
[total_grouped] => 2
[notifys] => Array ( [0] => Array ( [notifytype_id] => 12
[grouped] => 930
[icon] => icon.png
[n_url] => wall_action.php?id=930
[desc] => 690706096
[text] => Array ( [0] => Sarah O'conner ) [total] => 1 )))
Run Code Online (Sandbox Code Playgroud) 我正在尝试将元素从列表传递到for循环,当然我得到的经典错误'参数1必须是字符串而不是列表' - 对于os.chdir()函数.
这是我的代码,任何建议,我如何解决上述错误,仍然将我的列表的元素传递给脚本的其余部分,所以它循环通过每一个将非常感谢!!
path= ['C:\\DataDownload\Administrative', 'C:\\DataDownload\Cadastral', 'C:\\DataDownload\Development']
for x in path[:]:
os.chdir(path)
#Remove all previous files from the current folder
for file in os.listdir(path):
basename=os.path.basename(file)
if basename.endswith('.DXF'):
os.remove(file)
if basename.endswith('.dbf'):
os.remove(file)
if basename.endswith('.kmz'):
os.remove(file)
if basename.endswith('.prj'):
os.remove(file)
if basename.endswith('.sbn'):
os.remove(file)
if basename.endswith('.sbx'):
os.remove(file)
if basename.endswith('.shp'):
os.remove(file)
if basename.endswith('.shx'):
os.remove(file)
if basename.endswith('.zip'):
os.remove(file)
if basename.endswith('.xml'):
os.remove(file)
Run Code Online (Sandbox Code Playgroud) 当您在文件中迭代数百行时,在Python中运行正则表达式的最有效(最低效)方法是什么?
具体来说,是以下不良形式?
for line in file:
data = re.search('(\d+\.\d+)\|(-\d+\.\d+)\|(.*?)\|(.*?)\|(\d+:\d+\s+\w+)\sTO\s(.*?)',line)
one = data.group(1)
two = data.group(2)
three = data.group(3)
four = data.group(4)
five = data.group(5)
six = data.group(6)
# do the magic...
Run Code Online (Sandbox Code Playgroud) 我的示例字典是这样的
data_dictionary = {1:'blue',2:'green',3:'red',4:'orange',5:'purple',6:'mauve'}
Run Code Online (Sandbox Code Playgroud)
data_dictionary可以有更多元素,具体取决于传入的数据.第一个值是我们称之为payload_index的值.我总是得到payload_index 1到4.
我需要从中汇编一个列表.满容易:
for payload_index in data_dictionary :
assembled_packet.append(data_dictionary[payload_index])
Run Code Online (Sandbox Code Playgroud)
我的问题是我需要总是跳过第3个元素.我想我可以做一个if但这样效率很低:
for payload_index in data_dictionary :
if payload_index <> 3:
assembled_packet.append(data_dictionary[payload_index])
Run Code Online (Sandbox Code Playgroud)
我可以分两步完成前三个元素,但问题是我无法弄清楚如何获得其余的因为第三个元素之后的元素数量变化.我尝试使用一个不可能的高指数(下图)但显然失败了:
#get element 1 and two
for index in range(0,3):
assembled_packet.append(data_dictionary[index])
#get element 1 and two
for index in range(4,999):
assembled_packet.append(data_dictionary[index])
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?谢谢!
我必须读取两个csv文件,合并行并将结果写入第三个csv文件.第一个csv文件在第一个colunm中有五行带有用户名.(总共25个colunm)第二个csv文件在第一个colunm中有五行用户名,在第二个colunm中有用户id.(只有2个colunm)
第三个csv文件将包含用户名+ userid和第一个文件的所有剩余24列.
data = open(os.path.join("c:\\transales","AccountID+ContactID-source1.csv"),"rb").read().replace(";",",").replace("\0","")
data2 = open(os.path.join("c:\\transales","AccountID+ContactID-source2.csv"),"rb").read().replace(";",",").replace("\0","")
i = 0
j = 0
Info_Client_source1=StringIO.StringIO(data)
Info_Client_source2=StringIO.StringIO(data2)
for line in csv.reader(Info_Client_source1):
name= line[1]
i=i+1
print "i= ",i
for line2 in csv.reader(Info_Client_source2):
print "j = :",j
j=j+1
if line[1] == line2[2]:
continue
Run Code Online (Sandbox Code Playgroud)
结果:
i= 1
j = : 0
j = : 1
j = : 2
j = : 3
j = : 4
j = : 5
j = : 6
i= 2
i= 3
i= 4
i= 5 …Run Code Online (Sandbox Code Playgroud) 我想用参数启动一个python文件(.py)并在完成后接收它的输出.我已经听说过"popen"和"subprocess.call"但我找不到任何教程如何使用它们
有谁知道一个很好的教程?