python - 当前收到错误 TypeError: can only concatenate str (not "NoneType") to str
问题描述
Traceback (most recent call last):
File "<ipython-input-21-0cdf2cfacf71>", line 335, in <module>
+ my_value_a
TypeError: can only concatenate str (not "NoneType") to str
任何人都可以帮助解决这个错误吗?
代码
def get_env_var(i):
try:
letter = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'][i // 50]
return os.getenv("MY_VAR_" + letter)
except IndexError:
return "demo"
for i in range(0, len(mySymbols)):
try:
my_value_a = get_env_var(i)
#my_value_a = "demo"
#my_value_a = os.getenv("MY_VAR_K")
url_is_y = (
"https://financialmodelingprep.com/api/v3/financials/income-statement/"
+ mySymbols[i]
+ "?apikey="
+ my_value_a
)
url_bs_y = (
解决方案
So if the key for os.getenv()
is invalid, it returns the default values that you pass as the second parameter. If you don't set this default value, it returns a None
. Possible Fixes:
def get_env_var(i):
try:
letter = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'][i // 50]
return os.getenv("MY_VAR_" + letter, "demo")
except IndexError:
return "demo"
This will return the "demo" string if an invalid key is encountered. Or you can do this if the output is acceptable:
for i in range(0, len(mySymbols)):
try:
my_value_a = get_env_var(i)
#my_value_a = "demo"
#my_value_a = os.getenv("MY_VAR_K")
url_is_y = (
"https://financialmodelingprep.com/api/v3/financials/income-statement/"
+ mySymbols[i]
+ "?apikey="
+ str(my_value_a) # This will convert None to 'None'
)
url_bs_y = (
Check this page out for more info and examples on how this function works.
推荐阅读
- javascript - 在移动视图中隐藏 div 在 HTML 中不起作用
- mysql - Spring Boot with Hibernate:自动生成数据库模式,其列可以保存几分之一秒
- reactjs - {...{ key }} 是否与 key={key} 相同来分配反应属性?
- angular - 导出时如何增加 xlsx 表中特定列的字符限制?
- azure - 使用 Azure 读取 TCP 套接字
- video-streaming - 使用转向服务器作为中继使延迟很高
- swift - Swift - 按单元对 UICollectionView 进行分页,同时保持单元水平居中
- c++ - 无法专门化函数模板'未知类型 std::invoke(_Callable &&,_Types &&...) noexcept(
)' - android - codegen生成的adapter-constructor中的`moshi`是什么意思?
- java - spark-shell 命令错误:没有这样的文件或目录