I need to sort alphabetically a .txt file that contains an id and a content column like this:
SSADDS__234_234dvefeSADF, 1
SSFDS2342_234_dfsk___wewrew, 2
....
DFGFG__sasd_DFSD23423_3232, 3
Then I do the following to sort it:
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')
The file contain 500 lines (id and content). The problem with this is that when I run the above script I get 499 instead of 500, why this is happening and how can I fix it?
Just eliminate for
loop and you'll be good to go:
from string import strip
with open(raw_input("give me the file")) as f:
lines = sorted(map(strip, f))
with open(r'path', 'w') as a:
a.write('\n'.join(lines))
your problem is in for loop. just remove for loop and run your program without any other changes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.