字符串未完全读取

The String is Not Read Fully

我写了一个程序生成一串数字,由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,所有的值都会被读取。我无法弄清楚我的代码哪里出了问题。

谢谢

有些程序可能不喜欢文件不以换行符结尾的事实。在关闭之前尝试添加 files.write('\n')