首页 > 解决方案 > ValueError: int() 以 10 为底的无效文字:'\x00\x00\

问题描述

我有一个列表列表roots_3 = [['4', '5', '10'], ['11', '12', '13'], ['0', '17', '26'], ['1', '10', '15'], ['4', '19', '26'], ['11', '14', '22'],.... ] ,我想把里面的所有值都变成整数,所以我写了一个循环

for i in range(len(roots_3)):
    for j in range(len(roots_3[i])):
        roots_3[i][j]= np.int(roots_3[i][j])

这是我得到的错误:

ValueError: int() 以 10 为底的无效文字:'\x00\x00\x00...

请注意,roots_3 是从多个文本文件中获得的,如下所示:

for file in files_3:
    with open ("/Users/stordd/Desktop/StageI2M/Leiden/k3/{}".format(file), 'r+',encoding="utf8", errors='ignore') as f:
            lines = f.readlines()
            z = []
            for line in lines:

                value = line.split()
                num_lines = len(f.readlines())
                z.append(value[1])
            roots_3.append(z)
            f.close()

我从运行 C 程序的 python 脚本中获取了文本文件:

start = time.time()

cmd = ["/Users/stordd/Desktop/StageI2M/C/forestenostre/grezza_foresta", "-w","/Users/stordd/Desktop/StageI2M/Leiden/text_file/USA.txt", "-m", "3", "-e", "-0"]

ntrial = input("How many trials? ")
for i in range(int(ntrial)):

    # Open/Create the output file
    outFile = open("/Users/stordd/Desktop/StageI2M/Leiden/k3/{}.txt".format(i), 'ab')

    result = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    out = result.stdout.read()

    outFile.write(out)
    outFile.close()
    time.sleep(1)
end = time.time()
print(end - start) 

并且文本文件输出非常简单,它们看起来像这样:

基数 11
基数 14
基数 25

标签: python-3.7

解决方案


推荐阅读