I don't program in Python at all, so please forgive my code. I am trying to write a print function that terminates after a certain number of bytes. This is what I have done so far:
def print_stuff(stuff, size):
i = 0
data = ""
while i < size:
if stuff[i]=='\0':
data += " "
else:
data += stuff[i]
print data
But when I tried to do printf_stuff(data, 5050) Python doesn't print anything and seems to freeze. What am I doing wrong?
您没有增加i
的值。
You can do this with a string slice and a replace.
def print_stuff(stuff, size):
data = stuff[:size] # Slice it up to the size that you want.
data = data.replace('\0', ' ') # Replace all occurrences of \0 with a space.
print data
Yeah, you need to put i += 1 in there probably after your condition statements:
def print_stuff(stuff, size):
i = 0
data = ""
while i < size:
if stuff[i]=='\0':
data += " "
else:
data += stuff[i]
i += 1 # Here's your missing line.
print data
你没有增加i
变量
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.