python保存数据到文件

对于一个class里面定义的一个list ,list 里面顺序存储一个个set,如下:

定义在class的 def __init__(self):里面的一个list

# current landmark
self.curr_lmks = None

打印一帧数据可以看到里面的数据存储格式:
print(self.curr_lmks)
输出:
[[ 296.80924061  139.72795761]
 [ 322.215148    140.59542087]
 [ 347.24859795  143.71130616]
 [ 372.00324904  147.43723384]
 [ 394.80113752  155.18902678]
 [ 414.9305538   168.15372198]
 [ 431.30218886  184.80147068]
 [ 443.64536521  203.41504124]
 [ 447.14027354  225.05291964]
 [ 443.28832652  246.59363998]
 [ 430.71797592  264.75206486]
 [ 414.55095926  281.11598251]
 [ 394.65242666  294.1897381 ]
 [ 371.97248677  302.06383523]
 [ 347.88937108  306.67409222]
 [ 323.55717526  310.14117345]
 [ 298.49315548  311.96310889]
 [ 283.99451966  153.33874098]
 [ 272.41493546  164.13963954]
 [ 269.46621966  180.33307878]
 [ 272.12100384  197.18047545]
 [ 278.40835644  212.0666412 ]
 [ 276.98014556  237.77141369]
 [ 271.45553527  253.54568044]
 [ 269.64725658  269.74516075]
 [ 273.66807092  285.92694423]
 [ 285.28421434  296.96899469]
 [ 297.78244563  225.63579825]
 [ 313.8174885   225.34908485]
 [ 329.81669589  225.02463786]
 [ 346.47846266  224.91520497]
 [ 358.4812571   205.90181359]
 [ 361.68587905  214.74627043]
 [ 364.25123855  223.83015188]
 [ 362.19733902  233.34354907]
 [ 360.02786142  241.93427385]
 [ 299.02460418  171.08045808]
 [ 293.72807201  181.01709099]
 [ 294.38091032  192.35813271]
 [ 302.78048715  201.97298055]
 [ 304.32042702  191.7512151 ]
 [ 303.97649996  180.05676236]
 [ 303.15684361  247.58257376]
 [ 295.01206601  258.3793697 ]
 [ 295.26917961  269.62070089]
 [ 300.94121189  278.97420418]
 [ 305.40384661  270.35991267]
 [ 305.41826875  258.99514809]
 [ 389.80503977  190.25325085]
 [ 386.2454501   203.69397165]
 [ 383.81179746  215.67979947]
 [ 386.73336044  223.54923825]
 [ 384.62733147  232.35084859]
 [ 388.55912639  244.39104532]
 [ 392.16809038  256.7374104 ]
 [ 401.23753766  244.24678196]
 [ 405.25316831  232.59708244]
 [ 405.80244497  223.33753446]
 [ 404.53271651  214.73021935]
 [ 400.09129801  203.31302295]
 [ 390.59873412  195.06175848]
 [ 391.77573869  215.29000397]
 [ 393.35607505  223.26826962]
 [ 392.39806969  232.44697556]
 [ 392.50805944  251.56116003]
 [ 393.65204833  232.52086496]
 [ 394.38728346  223.68225595]
 [ 393.07540658  215.53054117]]

 可以看到一个个set 组合成一个list,因此要按照这个格式读进一个个文件。

version: 1
n_points: 68
{
307.397025383 174.430156899
 331.804216682 175.388580626
 356.690186475 178.531311836
 381.010974133 182.860924104
 405.05022191 189.438234189
 427.341033726 199.98372779
 445.670864088 216.042828932
 457.592641975 237.193008389
 460.075116671 263.487833871
 455.510316457 290.911816857
 442.83717274 314.56022734
 426.130537997 334.708180113
 404.874486553 349.486673911
 380.460838878 357.553700606
 354.378835892 361.646130926
 328.08664661 363.560754212
 302.013018174 364.21864476
 288.301426996 182.112606765
 275.361112246 192.003731416
 271.07088503 208.850184659
 272.489416253 226.960725095
 277.5323471 243.660718015
 275.169761543 270.044873064
 268.058836316 287.390249892
 265.094428378 305.994912089
 267.61472273 324.386717408
 278.111813924 338.595949501
 291.609241133 256.753812273
 306.091627608 256.041571961
 320.367298921 255.261105081
 335.493007772 254.414976777
 352.884488309 237.367406881
 355.048082219 246.818760318
 356.619595078 256.46932532
 354.208150036 267.736018467
 351.275628193 277.82987159
 299.689443987 200.987756331
 293.173525901 210.43218388
 292.490674502 222.34708885
 298.721927732 233.722888197
 301.445562176 222.944147953
 302.404713408 210.941696504
 295.90254648 285.024236385
 287.873807258 296.492105623
 287.990106171 308.918375125
 293.293389635 319.524819044
 296.820904726 309.779900547
 296.969855495 297.384900772
 395.758846885 224.609783372
 384.151243268 235.805215575
 378.294037423 248.139296314
 380.305974794 257.76304618
 377.527910534 268.253470524
 383.002662685 283.415578439
 392.516762014 297.847202239
 401.862277858 283.842293228
 405.631009862 269.38952889
 406.864028273 258.084133355
 406.486948011 247.776466117
 403.917088355 235.535662763
 394.901587608 230.578278488
 388.643749864 248.374225942
 388.760568018 257.77162377
 387.826657942 268.868047681
 392.420865945 291.652868114
 392.721752722 268.527897109
 393.848572811 257.59040575
 393.683940006 247.957171109
 }

 

首先一个video里面有多帧,因此需要一个全局变量来计数。在class的 def __init__(self):里面定义

self.cnt = 0
从0开始;
然后再保存的地方:
filename = "/Users/anitafang/Downloads/Datasets/video_Image_480_640/lyp/annot/%d.pts" % self.cnt
            f1 = open(filename, 'a')
            # f1.writelines( ['{:.2f}'.format(x) for x in self.curr_lmks])
            f1.writelines("version: 1\n")
            f1.writelines("n_points: 68\n")
            f1.writelines("{\n")
            for d in self.curr_lmks:
                f1.writelines(str(d[0]))
                f1.writelines(" ")
                f1.writelines(str(d[1]))
                f1.writelines("\n ")
            f1.writelines("}\n")
            f1.close()
            self.cnt=self.cnt+1

 

posted @ 2017-11-09 18:18  Anita-ff  阅读(1814)  评论(0编辑  收藏  举报