小编wja*_*rea的帖子

如果使用颜色提示,请查看如何在Python readline中修复列计算

我使用标准技巧来定制交互式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会话中调用历史搜索 - 向后搜索 …

python terminal interpreter readline

19
推荐指数
2
解决办法
2283
查看次数

VS Code Python 无法识别匹配语句

当我在 VS Code 中使用 Python 中的匹配大小写语句时,它会在“问题”选项卡中显示红色波浪线和错误:

如上所述 VS Code 的屏幕截图

python python-jedi visual-studio-code vscode-python python-3.10

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

有没有办法在 VSCode 编辑器或交互模式中自动完成 pandas 数据框列?

我正在使用 VScode 和交互模式来交互式编写 python 脚本。有什么方法可以自动完成 pandas 列名称,最好是在编辑器或交互式窗口中。目前其中任何一个都在工作。

我只能找到这个相关的帖子,该帖子已关闭而没有解决问题:https ://github.com/microsoft/vscode-jupyter/issues/1561

我还尝试回滚到此处建议的交互模式的旧实现: https: //github.com/microsoft/vscode-jupyter/issues/6995但这也不起作用。

欢迎任何解决此问题的建议,因为 VSCode 的其余部分似乎都非常棒。

python pandas visual-studio-code

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

如何遍历目录中的文件?

我有一个目录日志文件.我想使用Python脚本处理此目录中的每个文件.

for file in directory:
      # do something
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

python

16
推荐指数
3
解决办法
3万
查看次数

尝试索引数组时,具有多个元素的数组的真值是不明显的

如果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)

关于我做错的任何想法?

python numpy list-comprehension

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

查找具有重复值的字典键

__CODE__

我想把我们的词典的关键字归还给他们的价值,这些关键词存在了不止一次.

有人可以告诉我如何实现这个吗?

some_dict = {"firstname": "Albert", "nickname": "Albert", "surname": "Likins", "username": "Angel"}
Run Code Online (Sandbox Code Playgroud)

python dictionary

15
推荐指数
3
解决办法
4万
查看次数

VSCode Python 调试器突然停止

今天安装 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 秒。但随后它就关闭了。控制台中没有任何输出。

它不会在设置的断点处停止。

有人有同样的问题吗?有解决办法吗?

系统设置

  • 操作系统:Microsoft Windows 10 企业版(10.0.17763 内部版本 17763)
  • VSCode 版本 1.64.0
  • Python 版本:3.8.11(在活动的 Anaconda 环境中)

安装的 VSCode 扩展:

  • Python(微软)版本:v2022.0.1786462952
  • Pylance(微软)版本:v2022.2.0

python visual-studio-code

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

datetime64[ns] pandas 列的 apply(str) 和 astype(str) 的不同行为

我正在处理 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 datetime pandas

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

重新读取一个打开的文件Python

我有一个脚本读取文件,然后完成基于该文件的测试但是我遇到了问题,因为文件在一小时后重新加载,我无法让脚本在该时间点之后或之后重新读取该文件.

所以:

  • 获取新文件阅读
  • 读取文件
  • 执行文件测试
  • 获取新文件(具有相同名称 - 但如果它是解决方案的一部分,则可能会更改)
  • 读取新文件
  • 对新文件执行相同的测试

谁能建议让Python重新读取文件的方法?

python testing file

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

Python:获取和操作(作为整数)浮点数的位模式

在Python V.2.5.4中,我有一个浮点数,我想获取并操作(作为整数)浮点数的位模式.

例如,假设我有

x = 173.3125
Run Code Online (Sandbox Code Playgroud)

在IEEE 754格式中,x的位模式(十六进制)是x.

如何在该位模式上获取和操作(例如,执行按位操作)?

python floating-point bit-manipulation

12
推荐指数
2
解决办法
6087
查看次数