【发布时间】:2017-01-31 07:39:22
【问题描述】:
我编写了一个程序来生成一个数字字符串,由 0、1、2 和 3 组成,长度为 s,并将输出写入 decode.txt 文件。下面是代码:
import numpy as np
n_one =int(input('Insert the amount of 1: '))
n_two =int(input('Insert the amount of 2: '))
n_three = int(input('Insert the amount of 3: '))
l = n_one+n_two+n_three
n_zero = l+1
s = (2*(n_zero))-1
data = [0]*n_zero + [1]*n_one + [2]*n_two + [3]*n_three
print ("Data string length is %d" % len(data))
while data[0] == 0 and data[s-1]!=0:
np.random.shuffle(data)
datastring = ''.join(map(str, data))
datastring = str(int(datastring))
files = open('decode.txt', 'w')
files.write(datastring)
files.close()
print("Data string is : %s " % datastring)
当我尝试从另一个程序读取文件时出现问题,程序不调用字符串的最后一个值。
例如,如果生成的字符串是30112030000,则其他程序只会调用3011203000,即不调用最后的0。
但如果我直接在 .txt 文件中键入 30112030000,则会读取所有值。我不知道我的代码哪里出了问题。
谢谢
【问题讨论】:
标签: python python-2.7 call file-writing file-read