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

2024-04-26 00:46:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我是这个领域的新手,我想创建一个有用的快速处理程序,并运行这个脚本,我得到这个错误,我无法得到这个错误。

# -*- 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)

它发送这个错误

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

Tags: configjsonhostosftploginpassopen
2条回答
web_response = {2L: 67.0, 3L: 13.67, 4L: 10.25, 5L: 11.8, 6L: 11.83}

我有一个名为“web_response”的字典,用于将字典与我使用的逗号“,”

print "web_response=", web_response

输出:

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

错误消息应该是清楚的,不能使用+运算符对字符串和字典求和,可能需要将dict转换为字符串。如果getFTPConfig('Pass')返回字典,则

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

相关问题 更多 >