1

我正在尝试导入 sys 和 io:

import sys
import io

但我收到以下错误:

Error while compiling (offending file last):
    File '/Users/username/worktest/index.py', line 11, at import of:
    File '/Users/username/worktest/env/lib/python3.7/site-packages/numpy/__init__.py', line 293, at import of:
    File '/Users/username/opt/anaconda3/lib/python3.7/os.py', line 1026, at import of:
    File 'io', line 95, namely:
    
    Import error, can't find any of:
        /Users/username/worktest/_io/_WindowsConsoleIO.py
        /Users/username/worktest/_io/_WindowsConsoleIO.js
        /Users/username/worktest/env/lib/python3.7/site-packages/transcrypt/modules/_io/_WindowsConsoleIO.py
        /Users/username/worktest/env/lib/python3.7/site-packages/transcrypt/modules/_io/_WindowsConsoleIO.js
        /Users/username/worktest/_io/_WindowsConsoleIO.py
        /Users/username/worktest/_io/_WindowsConsoleIO.js
        /Users/username/opt/anaconda3/lib/python3.7/_io/_WindowsConsoleIO.py
        /Users/username/opt/anaconda3/lib/python3.7/_io/_WindowsConsoleIO.js
        /Users/username/opt/anaconda3/lib/python3.7/lib-dynload/_io/_WindowsConsoleIO.py
        /Users/username/opt/anaconda3/lib/python3.7/lib-dynload/_io/_WindowsConsoleIO.js
        /Users/username/worktest/env/lib/python3.7/site-packages/_io/_WindowsConsoleIO.py
        /Users/username/worktest/env/lib/python3.7/site-packages/_io/_WindowsConsoleIO.js
        /Users/username/worktest/_io.py
        /Users/username/worktest/_io.js
        /Users/username/worktest/env/lib/python3.7/site-packages/transcrypt/modules/_io.py
        /Users/username/worktest/env/lib/python3.7/site-packages/transcrypt/modules/_io.js
        /Users/username/worktest/_io.py
        /Users/username/worktest/_io.js
        /Users/username/opt/anaconda3/lib/python3.7/_io.py
        /Users/username/opt/anaconda3/lib/python3.7/_io.js
        /Users/username/opt/anaconda3/lib/python3.7/lib-dynload/_io.py
        /Users/username/opt/anaconda3/lib/python3.7/lib-dynload/_io.js
        /Users/username/worktest/env/lib/python3.7/site-packages/_io.py
        /Users/username/worktest/env/lib/python3.7/site-packages/_io.js


    Aborted
    
  ./index.py
Module build failed (from ./node_modules/transcrypt-loader/__target_es5__/index.js):
Error: Command failed: python3 -m transcrypt --nomin --map --verbose "index"
    at checkExecSyncError (child_process.js:630:11)
    at Object.execSync (child_process.js:666:15)
    at Object.main (/Users/username/worktest/node_modules/transcrypt-loader/__target_es5__/index.js:56:67)
Error: webpack returned an error. Try configuring `entry` in your webpack config relative to the current working directory, or setting `context = __dirname` in your webpack config.

我正在尝试使用 Transcrypt 和 Wrangler 让我的 Python 脚本在 Cloudflare 的 Workers 上运行,但是无法导入这些模块(pypi 上不存在)的问题阻止了我这样做。

在 pypi 中是否有任何替代方案可供我使用?如果没有,我如何在我的路径中本地安装它们。

它们的实际用法如下:

old_stdout = sys.stdout # Memorize the default stdout stream
sys.stdout = buffer = io.StringIO()

for row in query_job:
    # Row values can be accessed by field name or index.
    print("col1={}, col2={}, col3={}".format(row[0], row[1], row[2]))

sys.stdout = old_stdout # Put the old stream back in place

out1 = buffer.getvalue() # Return a str containing the entire contents of the buffer.

执行该操作以收集 Google BigQuery 查询的输出。这最终成为我能找到的实际检索输出的最简单方法,但我也愿意改变它。

4

2 回答 2

1

您使用非常非常奇怪的方法来创建字符串。

您不需要使用print()将其作为字符串保存在变量中 - 所以您不需要io.String()


更流行的是将所有字符串保留在列表中,然后使用"\n"

all_rows = []

for row in query_job:
    all_rows.append( "col1={}, col2={}, col3={}".format(row[0], row[1], row[2]) )

out1 = '\n'.join(all_rows)

哪个可以写为列表理解

all_rows = [ "col1={}, col2={}, col3={}".format(row[0], row[1], row[2]) for row in query_job ]

out1 = '\n'.join(all_rows)

不太流行的是直接使用字符串+=\n

out1 = ""

for row in query_job:
     out1 += "col1={}, col2={}, col3={}".format(row[0], row[1], row[2])
     out1 += "\n"

甚至可以"\n"直接放入"col1={}, col2={}, col3={}\n"

out1 = ""

for row in query_job:
     out1 += "col1={}, col2={}, col3={}\n".format(row[0], row[1], row[2])

顺便说一句:如果你只有 3 个元素,row那么你可以*使用.format(*row)

out1 = ""

for row in query_job:
     out1 += "col1={}, col2={}, col3={}\n".format(*row)

或列表理解

all_rows = [ "col1={}, col2={}, col3={}".format(*row) for row in query_job ]

out1 = '\n'.join(all_rows)

甚至在一行中

out1 = '\n'.join([ "col1={}, col2={}, col3={}".format(*row) for row in query_job ])

顺便提一句:

如果您必须使用print()缓冲区(或文件处理程序),那么您不必替换sys.stdout但您可以使用print(..., file=buffer)

buffer = io.StringIO()

for row in query_job:
    print("col1={}, col2={}, col3={}".format(row[0], row[1], row[2]), file=buffer)

out1 = buffer.getvalue() # 

但我不知道它是否可以与Transcrypt(and Wrangler) 一起使用,因为Transcrypt可能只有标准模块的一部分——只有可以转换为 JavaScript 的模块。某些函数可能无法转换,因为 JavaScript 无权访问您的磁盘(出于安全原因)并且它无法使用您的文件。

于 2020-07-30T17:03:34.407 回答
1

能够修改以前使用 pandas 收集输出的(效率非常低的)方法:

df = pd.read_gbq(sql, project_id=project_id)

我以前不知道的更快、更简单的解决方案。目前仍然与 Transcrypt 不兼容,但至少这部分得到了解决。

于 2020-07-30T18:28:18.523 回答