小编Mar*_*itz的帖子

py3k:你如何读取zip文件中的文件作为文本,而不是字节?

用于读取zip文件中的CSV文件的简单程序适用于Python 2.7,但不适用于Python 3.2

$ cat test_zip_file_py3k.py 
import csv, sys, zipfile

zip_file    = zipfile.ZipFile(sys.argv[1])
items_file  = zip_file.open('items.csv', 'rU')

for row in csv.DictReader(items_file):
    pass

$ python2.7 test_zip_file_py3k.py ~/data.zip

$ python3.2 test_zip_file_py3k.py ~/data.zip
Traceback (most recent call last):
  File "test_zip_file_py3k.py", line 8, in <module>
    for row in csv.DictReader(items_file):
  File "/home/msabramo/run/lib/python3.2/csv.py", line 109, in __next__
    self.fieldnames
  File "/home/msabramo/run/lib/python3.2/csv.py", line 96, in fieldnames
    self._fieldnames = next(self.reader)
_csv.Error: iterator should return strings, not bytes (did you open the file 
in text mode?)
Run Code Online (Sandbox Code Playgroud)

所以csvPython 3中的模块想要查看文本文件,但 …

csv zipfile python-3.x

31
推荐指数
3
解决办法
9363
查看次数

Python 3:如何获得字节字符串的字符串文字表示?

在Python 3中,如何将字节字符串插入到常规字符串中并获得与Python 2相同的行为(即:只获取没有b前缀或双反斜杠的转义码)?

例如:

Python 2.7:

>>> x = u'\u041c\u0438\u0440'.encode('utf-8')
>>> str(x)
'\xd0\x9c\xd0\xb8\xd1\x80'
>>> 'x = %s' % x
'x = \xd0\x9c\xd0\xb8\xd1\x80'
Run Code Online (Sandbox Code Playgroud)

Python 3.3:

>>> x = u'\u041c\u0438\u0440'.encode('utf-8')
>>> str(x)
"b'\\xd0\\x9c\\xd0\\xb8\\xd1\\x80'"
>>> 'x = %s' % x
"x = b'\\xd0\\x9c\\xd0\\xb8\\xd1\\x80'"
Run Code Online (Sandbox Code Playgroud)

注意如何使用Python 3,我b在输出中获得前缀和双下划线.我想得到的结果是我在Python 2中获得的结果.

python escaping python-3.x

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

如果当前令牌过期,有没有办法告诉 aws-cli 运行命令来获取新的会话令牌?

在我的工作中,我们的 AWS 身份验证与我们的企业登录 (SSO) 系统集成。换句话说,当我们想要访问 AWS 时,我们会执行一些操作,对我们的公司系统进行身份验证,然后颁发 AWS 会话令牌。令牌会在一小时后过期,因此 AWS 命令​​经常会因为令牌过期而失败,然后我必须获取新令牌,然后重复该命令。

这并不可怕,但由于我是一名工程师,我编写了一个“aws”包装脚本,用于检测令牌是否过期,如果过期,它可以运行可配置命令来获取新令牌,然后执行给出的命令。这是脚本:

#!/bin/bash

AWS=${AWS:-"/usr/local/bin/aws"}

if [ -z "$AWS_FORCE_GET_TOKEN" ] && ${AWS} sts get-caller-identity > /dev/null 2>&1; then
    ${AWS} "$@"
else
    CMD=$($AWS configure get get_token_command)
    echo "Error doing \"${AWS} sts get-caller-identity\"; Going to try running \"$CMD\"" > /dev/tty
    if $CMD 1>&2; then
        ${AWS} "$@"
    else
        echo "Failed to renew token with \"$CMD\"" 1>&2
    fi
fi
Run Code Online (Sandbox Code Playgroud)

该脚本看起来相当不错,但一个限制是它仅在调用“aws”可执行文件时才有效。boto3如果我有一个用于与 AWS 交互的Python 程序,那么它就不起作用。

现在我想知道我是否错过了 AWS 工具中内置的某些功能(botocore也许?)可以做到这一点?

我是一名 Python 程序员,所以如果它尚不存在并且被认为是一个好主意,我愿意添加它。但我想先检查一下它是否已经存在以及这是否是明智之举。

amazon-web-services aws-cli

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

奇怪的"BadZipfile:错误的CRC-32"问题

此代码简化了Django应用程序中的代码,该应用程序通过HTTP多部分POST接收上传的zip文件,并对内部数据进行只读处理:

#!/usr/bin/env python

import csv, sys, StringIO, traceback, zipfile
try:
    import io
except ImportError:
    sys.stderr.write('Could not import the `io` module.\n')

def get_zip_file(filename, method):
    if method == 'direct':
        return zipfile.ZipFile(filename)
    elif method == 'StringIO':
        data = file(filename).read()
        return zipfile.ZipFile(StringIO.StringIO(data))
    elif method == 'BytesIO':
        data = file(filename).read()
        return zipfile.ZipFile(io.BytesIO(data))


def process_zip_file(filename, method, open_defaults_file):
    zip_file    = get_zip_file(filename, method)
    items_file  = zip_file.open('items.csv')
    csv_file    = csv.DictReader(items_file)

    try:
        for idx, row in enumerate(csv_file):
            image_filename = row['image1']

            if open_defaults_file:
                z = zip_file.open('defaults.csv')
                z.close()

        sys.stdout.write('Processed %d items.\n' % …
Run Code Online (Sandbox Code Playgroud)

zipfile bytesio stringio

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

为什么“set -v”似乎在 bash 的子 shell 中不起作用?

看起来set -v在子 shell 中不起作用?为什么?

set -x确实可以在子 shell 中工作,但输出有点忙)。

$ cat foo.sh
set -v
date
set +v > /dev/null 2>&1

$ bash foo.sh
date
Fri Mar  3 14:52:34 PST 2017
set +v > /dev/null 2>&1

$ cat foo_subshell.sh
(
  set -v
  date
)

$ bash foo_subshell.sh
Fri Mar  3 14:52:42 PST 2017
Run Code Online (Sandbox Code Playgroud)

debugging bash shell

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