小编Gab*_*bip的帖子

TypeError:使用 Python 3.7 时,int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType”

我正在尝试运行下面的简单代码片段

port = int(os.getenv('PORT'))
print("Starting app on port %d" % port)
Run Code Online (Sandbox Code Playgroud)

我可以理解 PORT 是 s 字符串,但我需要转换为 int。为什么我收到错误

TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
Run Code Online (Sandbox Code Playgroud)

python environment-variables getenv python-3.x

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

ArgoCD 应用程序陷入同步/终止状态

我们\xe2\x80\x99在生产中面临一个奇怪的问题,其中一个应用程序陷入终止和同步状态。

\n

ArgoCD版本:2.1.7

\n

背景

\n

我们手动终止了应用程序的同步,现在应用程序陷入终止同步状态,并且禁用自动同步按钮也不起作用。

\n

注意:\n使用类似的配置创建一个新应用程序是可行的,但我们想调查当前应用程序卡住的原因。

\n

执行的步骤:

\n
    \n
  • 我们尝试从 UI 和 CLI 禁用自动同步
  • \n
  • 我们尝试终止该应用程序
  • \n
  • 我们尝试同步应用程序
  • \n
\n

已尝试的命令

\n
argocd app terminate-op APPNAME\nargocd app sync APPNAME\nargocd app sync APPNAME --force --prune \n
Run Code Online (Sandbox Code Playgroud)\n

一些日志(不确定这些是否相关):

\n
time="2022-01-07T16:19:36Z" level=debug msg="Failed to apply normalization: error in remove for path: \'/spec/preserveUnknownFields\': Unable to remove nonexistent key: preserveUnknownFields: missing value"\ntime="2022-01-07T16:19:36Z" level=debug msg="Failed to apply normalization: error in remove for path: \'/status\': Unable to remove nonexistent key: …
Run Code Online (Sandbox Code Playgroud)

argo argocd

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

有没有办法只搜索元组列表中的第一个坐标?

假设我有一个如下所示的元组列表:
a = [('a','b'), ('c','d'), ('e','f')]
如果我要执行这一行,'a' in a我会得到False.
有没有办法告诉python“只搜索第一个参数并接受第二个参数”?
这样我就可以搜索类似的东西('a', *) in a并得到True?

python tuples list python-3.x

4
推荐指数
2
解决办法
107
查看次数

如何在 presto 中将列转换为行?

我正在处理 presto sql 并被卡住了。

我想将列变成新行并保存值。

例如:

NAME  COMDEDY  HORROR  ROMANCE
brian    10     20     14
tom      20     10     11
Run Code Online (Sandbox Code Playgroud)

NAME    GANRE    RATING
brian   comedy     10
brian   horror     20
brian   romance    14
tom     comedy     20
tom     horror     10
tom     romance    11
Run Code Online (Sandbox Code Playgroud)

如果我不能用 prestodb 做到这一点,我至少需要用 python pandas 做到这一点

谢谢!

python sql unpivot pandas presto

4
推荐指数
1
解决办法
1004
查看次数

如何将dict的dict转换为指定格式的dict?

我有一本字典的字典,如下所示:

d = {0: {1: ["hello"], 2: ["How are you"]}, 1: {1: ["!"], 2: ["?"]}}
Run Code Online (Sandbox Code Playgroud)

我希望它采用所需的格式:

result = {1:["hello", "!"], 2: ["How are you", "?"]} 
Run Code Online (Sandbox Code Playgroud)

但是,我使用以下代码以以下格式获得此信息:

new_d = {}
for sub in d.values():
    for key, value in sub.items():
        new_d.setdefault(key, []).append(value)
Run Code Online (Sandbox Code Playgroud)

结果不具有所需的结构,并且会导致列表的列表。

{1: [['hello'], ['!']], 2: [['How are you'], ['?']]}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。谢谢。

python dictionary list data-structures

4
推荐指数
1
解决办法
159
查看次数

Python程序说文件已关闭但应该打开

我有这个 Python 程序:

#! python3
# random_quiz_generator.py - Creates quizzes with questions and answers in random order, along with the answer key.

import random

# The quiz data. Keys are states and values are their capitals
capitals = {'Alabama': 'Montgomery',
            .......
            'Wyoming': 'Cheyenne'
            }

# Generate 35 quiz files
for quiz_num in range(35):
    # create the quiz and answer key files
    quiz_file = open(f'capitalsquiz{quiz_num + 1}.txt', 'w')
    answer_key_file = open(f'capitalsquiz_answers{quiz_num +1}.txt', 'w')

    # write out the header for the quiz …
Run Code Online (Sandbox Code Playgroud)

python io

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

使用 Python 将维基百科数据提取到 txt 文件

我已经实现了以下代码来从维基百科页面提取数据

import bs4
import sys
import requests

res = requests.get('https://en.wikipedia.org/wiki/Agriculture' )
res.raise_for_status()
wiki = bs4.BeautifulSoup(res.text,"html.parser")
for i in wiki.select('p'):
    print(i.getText())
Run Code Online (Sandbox Code Playgroud)

该代码根据我的需要从页面中提取所有数据。但是我想使用 Python 将其存储在文本文件中,但我无法做到。该文本文件的名称应为“Agriculture”(如果从 URL 本身中提取该名称则更好,以便可以在多个 wiki 页面上使用)。

python io file list web-scraping

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

填充字典时出现 KeyError

我基本上需要以下格式的字典字典:

cable_footage = {"AER": {144: len1, 48: len2, 24: len3}, "UG": {144: len1, 48: len2, 24: len3}}
Run Code Online (Sandbox Code Playgroud)

这段代码是我到目前为止所拥有的:

cable_footage = defaultdict(dict)
    for placement in ("AER", "UG"):
        for size in (144, 48, 24):
            cable_footage[placement][size] = 0.0
    for cable in adss_layer.features:
        if cable.data["Placement"] == "AER":
            if cable.data["Size"] == 144:
                cable_footage["AER"][144] += cable.data["Length"]
            if cable.data["Size"] == 48:
                cable_footage["AER"][48] += cable.data["Length"]
        if cable.data["Placement"] == "UG":
            if cable.data["Size"] == 144:
                cable_footage["UG"][144] += cable.data["Length"]
            if cable.data["Size"] == 48:
                cable_footage["UG"][48] += cable.data["Length"]
Run Code Online (Sandbox Code Playgroud)

但是,我希望能够以这种方式简化/概括它,因为上面的代码很长且非 Pythonic:

for cable in …
Run Code Online (Sandbox Code Playgroud)

python dictionary python-3.x defaultdict

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