python读写问题

问题描述

我正在尝试从我的.json文件提取一些信息,并将其保存到.txt文件中。一切顺利,直到我打开.txt文件并发现该文件输出与所需的输出不同。这是我的编码。

import numpy as np
import json
from numpy import *

with open('myjson.json','rb') as f:
   data = json.load(f)
   alllist=[]
   qq=[0,1,2]
   for classes in range(len(data["shapes"])):   
       x1 = data["shapes"][classes]["points"][0][0]
       y1 = data["shapes"][classes]["points"][0][1]
       x2 = data["shapes"][classes]["points"][1][0]
       y2 = data["shapes"][classes]["points"][1][1]
       c = data["shapes"][classes]["label"]
       if c=='T':
           c=qq[2]
       elif c=="FAN":
           c=qq[1]
       elif c=="DIMM":
           c=qq[0]
               
       alist = [int(x1),int(y1),int(x2),int(y2),int(c)]
       alllist.append(alist)
       
   final = np.reshape(alllist,(13,5))    
   print(final,file = open('mytest.txt','a'))

输出

[[446 438 673 738   2]
 [813  97 694 239   1]
 [689  97 586 232   1]
 [584  93 474 233   1]
 [469  90 355 232   1]
 [783 345 758 748   0]
 [754 343 731 746   0]
 [728 346 710 744   0]
 [704 346 676 747   0]
 [441 343 414 742   0]
 [411 343 385 743   0]
 [384 341 361 739   0]
 [360 341 334 743   0]]

当我选择print(final[0])时,输出

[446 438 673 738   2]

但是,当我读取文件并打印相同的代码时,它给出的输出

mytest = open ('mytest.txt','rb')
result = mytest.read()
print(result[0])
91

如何使'mytest.txt'的输出与开头的print(final[0])相同?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)