将列表写入文件时出现意外输出

时间:2016-03-14 16:48:10

标签: python io

代码的相关部分

result1 = result[::4]
print result1

thefile=open('freq.txt','wb')
thefile.writelines(["%s\n" % item  for item in result1])

我想获取每个第四个元素表单结果并将其写入freq.txt。

在屏幕上我得到了正确的输出

['56337.19531', '35788.30469', '24740.00000', '16800.32031', '10672.43359', '7886.67725', '5010.02539', '3318.00000', '2351.88672', '1494.04028', '1026.00000', '701.35638', '445.53790', '318.00000', '209.15157', '132.86391', '98.00000', '62.37111', '40.00000', '29.27925', '18.59970', '12.00000', '8.73137', '5.54662', '3.90000', '2.60378', '1.65406', '1.20000', '0.77647', '0.50000', '0.36451', '0.23155', '0.15480', '0.10870', '0.06905', '0.05000', '0.03242', '0.02059', '0.01471', '0.00967', '0.00617', '0.00454', '0.00288']

结果

['56337.19531', '48429.55859', '44960.00000', '41631.85938', '35788.30469', '33180.00000', '30764.96289', '26446.71289', '24740.00000', '22734.58398', '19543.49805', '18640.00000', '16800.32031', '14442.18457', '13760.00000', '12415.04102', '10672.43359', '10260.00000', '9174.42285', '8000.00000', '7886.67725', '6779.68262', '5996.00000', '5828.06836', '5010.02539', '4496.00000', '4306.80518', '3702.29077', '3318.00000', '3182.62769', '2735.90576', '2474.00000', '2351.88672', '2021.76978', '1864.00000', '1737.98889', '1494.04028', '1376.00000', '1284.33289', '1104.06055', '1026.00000', '949.09174', '815.87469', '766.00000', '701.35638', '602.91217', '572.00000', '518.28589', '445.53790', '426.00000', '383.00104', '329.24200', '318.00000', '283.02875', '243.30209', '236.00000', '209.15157', '179.79451', '176.00000', '154.55807', '132.86391', '132.00000', '114.21478', '98.18330', '98.00000', '84.40203', '74.00000', '72.55514', '62.37111', '57.00000', '53.61654', '46.09078', '40.00000', '39.62136', '34.06000', '30.00000', '29.27925', '25.16954', '22.00000', '21.63668', '18.59970', '15.98899', '15.00000', '13.74474', '12.00000', '11.81549', '10.15704', '9.40000', '8.73137', '7.50581', '7.00000', '6.45228', '5.54662', '5.20000', '4.76808', '4.09882', '3.90000', '3.52350', '3.02893', '2.91000', '2.60378', '2.23831', '2.17000', '1.92413', '1.65406', '1.61000', '1.42189', '1.22231', '1.20000', '1.05074', '0.90909', '0.90326', '0.77647', '0.67568', '0.66749', '0.57380', '0.50000', '0.49326', '0.42402', '0.37037', '0.36451', '0.31334', '0.27778', '0.26936', '0.23155', '0.20747', '0.19905', '0.17111', '0.15480', '0.14709', '0.12645', '0.11628', '0.10870', '0.09344', '0.08333', '0.08033', '0.06905', '0.06250', '0.05936', '0.05103', '0.05000', '0.04386', '0.03771', '0.03571', '0.03242', '0.02787', '0.02632', '0.02395', '0.02059', '0.02000', '0.01770', '0.01522', '0.01471', '0.01308', '0.01124', '0.01111', '0.00967', '0.00831', '0.00820', '0.00714', '0.00617', '0.00614', '0.00528', '0.00459', '0.00454', '0.00390', '0.00342', '0.00335', '0.00288', '0.00255', '0.00248', '0.00213']

但是我的freq.txt文件有180个元素。

 wc -l freq.txt
180 freq.txt

我尝试将result1重命名为new1并使用循环

for item in new1:
  thefile.write("%s\n" % item)

但问题是一样的。

print len(result1) 43,因为它应该是

为什么?

0 个答案:

没有答案