douwen0612 2013-01-21 12:43
浏览 77
已采纳

开始使用python问题

So, I just started with python and have learned how to fix some issues but I got stuck with this one and what I have searched, doesn't help me.

This is my test.py file: http://pastebin.com/TzASyCxm where I work with mysql and output some data to an LCD screen.

Right now, executing the code in my script returns the following error:

self.ip[host] = socket.gethostbyname(host)
TypeError: 'str' object does not support item assignment

I'm trying to do like a simple, in php:

$data = array();
$test = 'level1';
$data[$test] = 'alex';
  • 写回答

2条回答 默认 最新

  • douzhe3516 2013-01-21 12:51
    关注

    As in PHP, where you need to initialize the variable $data with an empty array array(), so as to index and assign elements to it, in similar ways, you need to assign a dictionary to self.ip to subsequently index and assign values.

    The ways you can do it varies with requirement, but we generally stick to one of the two formats

    self.ip = dict()
    self.ip = {}
    

    Note

    Python Error Messages are self explanatory and you can easily identify the Issue from it

    TypeError: 'str' object does not support item assignment
    

    It means self.ip is a string object, and in Python, String Objects are immutable and cannot be indexed unlike C/C++. So likely, you may have initialized this variable somewhere prior to the failure with a string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?