2015-02-24 64 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