我使用标准技巧来定制交互式Python会话:
$ cat ~/.bashrc
export PYTHONSTARTUP=~/.pystartup
$ cat ~/.pystartup
import os
import sys
import atexit
import readline
import rlcompleter
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
term_with_colors = ['xterm', 'xterm-color', 'xterm-256color', 'linux', 'screen', 'screen-256color', 'screen-bce']
if os.environ.get('TERM') in term_with_colors:
green='\033[32m'
red='\033[31m'
reset='\033[0m'
sys.ps1 = red + '>>> ' + reset
sys.ps2 = green + '... ' + reset
del term_with_colors
atexit.register(save_history)
del os, sys, atexit, readline, rlcompleter, save_history, historyPath
现在我得到上下文敏感的完成和颜色提示.
问题来自颜色提示 - 当我在交互式Python会话中调用历史搜索 - 向后搜索 …
当我在 VS Code 中使用 Python 中的匹配大小写语句时,它会在“问题”选项卡中显示红色波浪线和错误:

python python-jedi visual-studio-code vscode-python python-3.10
我正在使用 VScode 和交互模式来交互式编写 python 脚本。有什么方法可以自动完成 pandas 列名称,最好是在编辑器或交互式窗口中。目前其中任何一个都在工作。
我只能找到这个相关的帖子,该帖子已关闭而没有解决问题:https ://github.com/microsoft/vscode-jupyter/issues/1561
我还尝试回滚到此处建议的交互模式的旧实现: https: //github.com/microsoft/vscode-jupyter/issues/6995但这也不起作用。
欢迎任何解决此问题的建议,因为 VSCode 的其余部分似乎都非常棒。
我有一个目录日志文件.我想使用Python脚本处理此目录中的每个文件.
for file in directory:
# do something
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
如果var(另一个numpy数组)中的元素> = 0且<=.1,我试图将rbs的所有元素放入一个新数组中.但是,当我尝试以下代码时,我收到此错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Run Code Online (Sandbox Code Playgroud)
rbs = [ish[4] for ish in realbooks]
for book in realbooks:
var -= float(str(book[0]).replace(":", ""))
bidsred = rbs[(var <= .1) and (var >=0)]
Run Code Online (Sandbox Code Playgroud)
关于我做错的任何想法?
__CODE__
我想把我们的词典的关键字归还给他们的价值,这些关键词存在了不止一次.
有人可以告诉我如何实现这个吗?
some_dict = {"firstname": "Albert", "nickname": "Albert", "surname": "Likins", "username": "Angel"}
Run Code Online (Sandbox Code Playgroud) 今天安装 Windows 更新后,调试不再起作用。
这是我的主动调试配置:
"launch": {
"version": "0.2.0",
"configurations": [
{
"name": "DEBUG CURR",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole",
"justMyCode": false,
"stopOnEntry": false,
}...
Run Code Online (Sandbox Code Playgroud)
当我启动调试器时,菜单会短暂弹出 1-2 秒。但随后它就关闭了。控制台中没有任何输出。
它不会在设置的断点处停止。
有人有同样的问题吗?有解决办法吗?
安装的 VSCode 扩展:
我正在处理 pandas 中的日期时间信息,并希望将一堆datetime64[ns]列转换为str. 我注意到这两种方法的行为有所不同,我预计会产生相同的结果。
这是一个MCVE。
import pandas as pd
# Create a dataframe with dates according to ISO8601
df = pd.DataFrame({"dt_column": ["2023-01-01", "2023-01-02", "2023-01-02"]})
# Convert the strings to datetimes
# (I expect the time portion to be 00:00:00)
df["dt_column"] = pd.to_datetime(df["dt_column"])
df["str_from_astype"] = df["dt_column"].astype(str)
df["str_from_apply"] = df["dt_column"].apply(str)
print(df)
print()
print("Datatypes of the dataframe")
print(df.dtypes)
Run Code Online (Sandbox Code Playgroud)
输出
dt_column str_from_astype str_from_apply
0 2023-01-01 2023-01-01 2023-01-01 00:00:00
1 2023-01-02 2023-01-02 2023-01-02 00:00:00
2 2023-01-02 2023-01-02 2023-01-02 00:00:00 …Run Code Online (Sandbox Code Playgroud) 我有一个脚本读取文件,然后完成基于该文件的测试但是我遇到了问题,因为文件在一小时后重新加载,我无法让脚本在该时间点之后或之后重新读取该文件.
所以:
谁能建议让Python重新读取文件的方法?
在Python V.2.5.4中,我有一个浮点数,我想获取并操作(作为整数)浮点数的位模式.
例如,假设我有
x = 173.3125
Run Code Online (Sandbox Code Playgroud)
在IEEE 754格式中,x的位模式(十六进制)是x.
如何在该位模式上获取和操作(例如,执行按位操作)?
python ×10
pandas ×2
datetime ×1
dictionary ×1
file ×1
interpreter ×1
numpy ×1
python-3.10 ×1
python-jedi ×1
readline ×1
terminal ×1
testing ×1