小编hee*_*ayl的帖子

如何从Python中的zip文件中读取zip文件?

我有一个我想要阅读的文件,它本身是在zip存档中压缩的.例如,parent.zip包含child.zip,其中包含child.txt.我在阅读child.zip时遇到了麻烦.谁能纠正我的代码?

我假设我需要创建一个类似文件的对象的child.zip,然后用第二个zipfile实例打开它,但是对于python我是新的zipfile.ZipFile(zfile.open(name))是愚蠢的.它引发了一个zipfile.BadZip文件:"文件不是一个zip文件"on(独立验证)child.zip

import zipfile
with zipfile.ZipFile("parent.zip", "r") as zfile:
    for name in zfile.namelist():
        if re.search(r'\.zip$', name) is not None:
            # We have a zip within a zip
            with **zipfile.ZipFile(zfile.open(name))** as zfile2:
                    for name2 in zfile2.namelist():
                        # Now we can extract
                        logging.info( "Found internal internal file: " + name2)
                        print "Processing code goes here"
Run Code Online (Sandbox Code Playgroud)

python zip file unzip

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

在bash中为$ @添加前缀和后缀

如何添加后缀和前缀$@

如果我这样做$PREFIX/$@/$SUFFIX,我只在第一个参数中得到前缀和后缀.

bash

12
推荐指数
3
解决办法
4413
查看次数

chmod 777 到 python 脚本

尝试将 linux cmd 转换为 python 脚本

Linux 命令:

chmod 777 file1 file1/tmp file2 file2/tmp file3 file3/tmp
Run Code Online (Sandbox Code Playgroud)

我知道,os.chmod(file, 0777)但我不确定如何将它用于上述行。

python linux chmod

6
推荐指数
1
解决办法
1万
查看次数

理解Python中的're.search()'行为

这是我用来从一串字母数字中分割字母和数字的python代码:

input_string = 'abcdefghijklmnopqrstuvwxyz1234567890'
import re
print re.search('[a-z]*', input_string).group()
print re.search('[0-9]*', input_string).group()
Run Code Online (Sandbox Code Playgroud)

在输出中我得到字母串但没有得到数字串.如果我修改代码如下,则输出显示数字:

print re.search('[0-9]*$', input_string).group()
Run Code Online (Sandbox Code Playgroud)

我习惯了grep,我发现它的功能类似于re模块的功能,如果我在shell中运行以下命令,我会得到所需的结果:

echo "abcdefghijklmnopqrstuvwxyz1234567890" | grep "[0-9]*"
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

python regex python-2.7

4
推荐指数
1
解决办法
2027
查看次数

如何剥离[]中的所有内容

我试图剥离内部的主要文本,[]包括[]如下

title  = "[test][R] D123/Peace123456: panic:"
print title
title = title.strip('[.*]')
print title
Run Code Online (Sandbox Code Playgroud)

输出: -

test][R] D123/Peace123456: panic:
Run Code Online (Sandbox Code Playgroud)

预期产量:

[R] D123/Peace123456: panic:
Run Code Online (Sandbox Code Playgroud)

python regex

4
推荐指数
1
解决办法
54
查看次数

如何通过方法透明地查看函数属性

我最近编写了许多需要标记属性的函数:

def fn1(): pass
fn1.mark = True
Run Code Online (Sandbox Code Playgroud)

实际的标记是由装饰者完成的,但它既不在这里也不在那里.我担心的是,当我以相同的方式在类中标记方法时,绑定方法时标记将不可见:

class A:
    def meth1(): pass
    meth1.mark = True
Run Code Online (Sandbox Code Playgroud)

但实际上该属性可见:

>>> fn1.mark
True
>>> A.meth1.mark
True
>>> A().meth1.mark
True
Run Code Online (Sandbox Code Playgroud)

但是,无法在绑定方法中分配或删除该属性,因为它可以在函数中:

>>> A().meth1.mark = False
AttributeError: 'method' object has no attribute 'mark'

>>> del A().meth1.mark
AttributeError: 'method' object has no attribute 'mark'
Run Code Online (Sandbox Code Playgroud)

方法的属性在绑定时如何变为可见?

python methods attributes

4
推荐指数
1
解决办法
37
查看次数

解析网络地址字符串

我正在使用python 3.5并具有如下所示的网络地址字符串:

tcp://10.1.2.3:45678
Run Code Online (Sandbox Code Playgroud)

我想解析这个字符串并提取协议,IP地址和端口号.

我知道我可以使用字符串拆分或正则表达式轻松完成此操作,但我想知道是否有一个python包或模块执行此操作.我确信这些字符串有一个规范来定义它们,因此我对python模块感兴趣,而不是使用正则表达式或字符串解析.

python parsing

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

读取"和\之间的值

我有一个字符串 "X[2] <= 17055.5\ngini = 0.0454\nsamples = 43\nvalue = [42, 1]"

我想阅读X[2] <= 17055.5 任何帮助将不胜感激,我知道它是使用python正则表达式完成的.我是那个话题的新手.

python regex python-3.x

0
推荐指数
1
解决办法
49
查看次数

标签 统计

python ×7

regex ×3

attributes ×1

bash ×1

chmod ×1

file ×1

linux ×1

methods ×1

parsing ×1

python-2.7 ×1

python-3.x ×1

unzip ×1

zip ×1