我正在尝试导入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查询的输出。这是我能找到的最简单的方法来实际检索输出,但我也愿意修改它。
您使用非常非常奇怪的方法来创建字符串。
您不需要使用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])
BTW:如果row
中只有3个元素,则可以在.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 ])
BTW:
如果必须使用带缓冲区(或带文件处理程序(的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
(和Wrangler
(,因为Transcrypt
可能只有标准模块的一部分——只有可以转换为JavaScript的模块。有些函数可能无法转换,因为JavaScript无法访问您的磁盘(出于安全原因(,也无法使用您的文件。
能够重新使用pandas收集输出的以前(效率很低(的方法:
df = pd.read_gbq(sql, project_id=project_id)
更快、更简单的解决方案,这是我以前不知道的。目前仍然与Transcrypt不兼容,但至少解决了这一部分。