【发布时间】:2015-05-01 10:51:51
【问题描述】:
我需要按字母顺序对包含 id 和内容列的 .txt 文件进行排序,如下所示:
SSADDS__234_234dvefeSADF, 1
SSFDS2342_234_dfsk___wewrew, 2
....
DFGFG__sasd_DFSD23423_3232, 3
然后我做以下排序:
f=open(raw_input("give me the file"))
for word in f:
l = sorted(map(str.strip, f))
#print "\n",l
a = open(r'path', 'w')
#a.writelines(l)
a.write('\n'.join(l)+'\n')
该文件包含 500 行(id 和内容)。问题在于,当我运行上述脚本时,我得到 499 而不是 500,为什么会发生这种情况,我该如何解决?
【问题讨论】:
-
为什么要在 for 循环中排序?另外,你得到索引 499,例如。 f[499] 或者你得到长度 499,len(f) == 499?
-
因为这是我解决这个问题的最佳方法......关于如何排序 alfabeticaly 这个的任何其他想法?感谢您的反馈!
标签: python python-2.7 sorting io