Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python 3.3中按字母顺序从文件排序_Python_Python 3.3 - Fatal编程技术网

在python 3.3中按字母顺序从文件排序

在python 3.3中按字母顺序从文件排序,python,python-3.3,Python,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') fi

帮助我这是我当前的代码

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的文本文件中进行排序,我还试图将其从数字中的最高点到最低点进行排序

如果行都是整数,则需要转换为整数并反转排序:

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

如果不强制转换为int,那么22>100

当前代码有什么问题?您是否有任何错误?Class1.txt看起来像什么?生成器表达式错误并突出显示nothing@bobabu,错过了最近一次的内部parensTraceback调用:programs\test3.py,第7行,在sortedinti中为i in f,reverse=True-ValueError:对关闭的文件执行I/O操作。那么您没有在with语句中执行此操作。您能解释一下您的意思吗