Python 类型错误:无法连接“str”和“dict”对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26632186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:47:02  来源:igfitidea点击:

TypeError: cannot concatenate 'str' and 'dict' objects

pythonpython-2.7

提问by Loukino

I am a novice in the field, and I wanted to create a program that was useful and fast handling, and run this script, I get this error, and I could not get this error.

我是该领域的新手,我想创建一个有用且处理速度快的程序,然后运行此脚本,出现此错误,但无法获取此错误。

# -*- coding: cp1252 -*-
import ftplib, os
from ftplib import FTP 

def getFTPConfig(FTPLib):
    if os.path.exists("./incluide/Config.json"):
        with open("./incluide/Config.json") as ConfigFTP:
            FTPConnect = ConfigFTP.read()
    else:
        os.system("title Config FTPConnect")
        Host = raw_input("Enter the host: ")
        Login = raw_input("Enter the login: ")
        Pass = raw_input("Enter de password: ")
        file = open("./incluide/Config.json", "wb")
        file.write('''{"Host": "'''+Host+'''",
 "Login: "'''+Login+'''",
 "Pass: "'''+Pass+'''"}''')
        file.close()
        print "Successfully created"
        os.system("FTP Session")
    try:
        FTPs = json.loads(FTPConnect)
        return FTPs[FTPLib]
    except:
        return {}

    with open("./incluide/Config.json") as ConfigFTP:
        FTPConnect = ConfigFTP.read()
        print('Conectando ao Servidor FTP... Espere um momento...')

        FTPConnect = FTP(Host, Login, Pass)

        File = "Session.py" #Arquivo a ser enviado

        file = open('%s' %(File),'rb')
        print('Conectado.')

        print('Enviando arquivo... Espere um momento...')

        session.storbinary('STOR %s' %(File), file)

        print('Arquivo enviado!')

        file.close()
        session.quit()

if __name__ == "__main__":
    Title = "ERROR CONNECTING TO FTP"
    os.system('cls');os.system('title '+Title)
    FTPError = """
               """+Title+"""
               Check the connection:
               Host: """+getFTPConfig('Host')+"""
               Login: """+getFTPConfig('Login')+"""
               Pass: """+getFTPConfig('Pass')+""""""
    print (FTPError).center(80)
    time.sleep(5)

It sends this error

它发送此错误

Traceback (most recent call last):
  File "C:\Users\Desktop\FTP\Session.py", line 59, in <module>
    Pass: """+getFTPConfig('Pass')+""""""
TypeError: cannot concatenate 'str' and 'dict' objects

采纳答案by lelloman

the error message should be clear, you can't use +operator to sum a string and a dictionary, maybe you want to convert the dict to a string. If getFTPConfig('Pass') is returning a dictionary then

错误信息应该很清楚,您不能使用+运算符对字符串和字典求和,也许您想将 dict 转换为字符串。如果 getFTPConfig('Pass') 返回一个字典,那么

str(getFTPConfig('Pass'))+"whatever"

回答by Taimoor Changaiz

web_response = {2L: 67.0, 3L: 13.67, 4L: 10.25, 5L: 11.8, 6L: 11.83}

I've a dictionary named "web_response",For concatenation of dictionary with stringI used comma","

我有一本名为“web_response”的字典,为了将字典与字符串连接起来,我使用了逗号“,”

print "web_response=", web_response

Output:

输出:

web_response= {2L: 67.0, 3L: 13.67, 4L: 10.25, 5L: 11.8, 6L: 11.83}