2015-02-24 52 views
0

幫助我,這是我當前的代碼按字母順序排序從一個文件在python 3.3

p=('class1.txt') 
file=open(p,'r') 
d=file.read() 
print(d) 
with open('Class1.txt', 'r') as f: 
    Class1=sorted(Class1.rstrip('\n') for Class1 in f) 
print(Class1) 
q=('class2.txt') 
file=open(q,'r') 
f=file.read() 
print(f) 

w=('class3.txt') 
file=open(w,'r') 
g=file.read() 
print(g) 

我試圖從名爲Class1.txt我也在努力,使其在最高到一個文本文件進行排序從數字最低

+3

當前代碼有什麼問題?你有任何錯誤? – 2015-02-24 15:11:29

+1

Class1.txt是什麼樣的? – Andy 2015-02-24 15:11:42

回答

0

如果行,你需要轉換爲int和反向排序的所有整數:

with open('Class1.txt', 'r') as f: 
    class_1 = sorted((int(i) for i in f),reverse=True) 
    print(class_1) 

如果不轉換爲整數,那麼"22" > "100"

+0

發電機表達錯誤,它強調什麼 – 2015-02-24 15:19:18

+0

@bobabu,錯過了內部括號 – 2015-02-24 15:20:07

+0

回溯(最近通話最後一個): 程序\ test3.py」,7號線,在 排序((INT(i)對於我f中),reverse = True) ValueError:關閉文件上的I/O操作 – 2015-02-24 15:21:54