小编Sil*_*ost的帖子

正则表达式整个输入匹配模式

如何使用PHP preg_replace函数制作正则表达式模式,删除所有不适合某种模式的字符.例如:

[a-zA-Z0-9]
Run Code Online (Sandbox Code Playgroud)

php regex

2
推荐指数
1
解决办法
156
查看次数

正则表达式

我想要匹配这个:

  • 八(8)个字母数字字符
  • 接着 - 连字符
  • 接下来是二十二(22)个字母数字字符,这是我尝试过的,它不匹配:

[8]\w+-[22]\w+

regex

2
推荐指数
1
解决办法
101
查看次数

为什么id(字符串)不返回字符串的地址

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'.我不知道为什么.有谁能解释我?

python ctypes

2
推荐指数
1
解决办法
260
查看次数

python文件读取

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命令提示符中运行它们时 - 我得到预期的输出!

python file

2
推荐指数
1
解决办法
5257
查看次数

我怎么能访问这样的PHP数组

我试图访问这个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)

php arrays

2
推荐指数
1
解决办法
108
查看次数

将列表元素传递给for循环

我正在尝试将元素从列表传递到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

2
推荐指数
1
解决办法
998
查看次数

在Python循环中重复使用正则表达式的最有效方法是什么?

当您在文件中迭代数百行时,在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)

python regex

2
推荐指数
1
解决办法
160
查看次数

仅使用Python访问字典中的部分字典

我的示例字典是这样的

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)

有任何想法吗?谢谢!

python

2
推荐指数
1
解决办法
1588
查看次数

嵌套for循环的问题

我必须读取两个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

2
推荐指数
1
解决办法
2264
查看次数

用参数调用程序

我想用参数启动一个python文件(.py)并在完成后接收它的输出.我已经听说过"popen"和"subprocess.call"但我找不到任何教程如何使用它们

有谁知道一个很好的教程?

python python-3.x

2
推荐指数
2
解决办法
6081
查看次数

标签 统计

python ×7

regex ×3

php ×2

arrays ×1

ctypes ×1

file ×1

python-3.x ×1