我正在尝试将字典写入txt文件.然后通过键入键来读取dict值raw_input
.我觉得我只是错过了一步,但我一直在寻找一段时间.
我收到这个错误
File "name.py", line 24, in reading
print whip[name]
TypeError: string indices must be integers, not str
Run Code Online (Sandbox Code Playgroud)
我的代码:
#!/usr/bin/env python
from sys import exit
class Person(object):
def __init__(self):
self.name = ""
self.address = ""
self.phone = ""
self.age = ""
self.whip = {}
def writing(self):
self.whip[p.name] = p.age, p.address, p.phone
target = open('deed.txt', 'a')
target.write(str(self.whip))
print self.whip
def reading(self):
self.whip = open('deed.txt', 'r').read()
name = raw_input("> ")
if name in self.whip:
print self.whip[name]
p = Person()
while …
Run Code Online (Sandbox Code Playgroud) 我有一个使用ftplib的ftp程序,在过去的几天里,我一直试图弄清楚如何实现retrbinary和storbinary的进度条.我正在尝试使用进度条2.3模块,但却没有把它与它结合起来.我认为这可能源于对回调函数如何工作的误解.无论如何这里是我的.
import progressbar
import ftplib
ftp = ftplib.FTP()
ftp.connect("host", "port")
ftp.login("user", "pwd")
widgets = ['Downloading: ', Percentage(), ' ', Bar(marker=RotatingMarker()),
' ', ETA(), ' ', FileTransferSpeed()]
pbar = ProgressBar(widgets=widgets, maxval=1024).start()
def callback(p):
for i in range(1024):
pbar.update(10*i+1)
pbar.finish()
ftp.storbinary("STOR iTunesSetup.exe", open("iTunesSetup.exe"), callback, blocksize=1024)
Run Code Online (Sandbox Code Playgroud)
我收到此错误,虽然这只是我尝试过的众多方法之一:
Traceback (most recent call last):
File "progrbar.py", line 7, in <module>
ftp.connect("host", "port")
File "/usr/lib/python2.7/ftplib.py", line 132, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, …
Run Code Online (Sandbox Code Playgroud) 所以我要做的是制作人员及其信息的字典,但我想用他们的名字作为主键,并让他们的信息的每一部分也有一把钥匙.我无法弄清楚如何改变他们个人信息的价值.我甚至不确定我是否正确的方法这里是代码.
name = raw_input("name")
age = raw_input("age")
address = raw_input("address")
ramrod = {}
ramrod[name] = {'age': age}, {'address' : address}
print ramrod
#prints out something like this: {'Todd': ({'age': '67'}, {'address': '55555 FooBar rd'})}
Run Code Online (Sandbox Code Playgroud)