无法排序多行 [英] Unable to sort multiple lines

查看:106
本文介绍了无法排序多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标:使用Python对文本文件中的多行进行排序

My aim: to sort multiple lines in a text file using Python

方法

已创建一个示例文件,该文件用于测试Python脚本

A sample file has been created and this is used to test the Python script

代码:

f = open("C:\\sample.txt", "r")
lines = f.readlines()
print(lines)
print(lines.sort)

Sample.txt

gg
bb
aa
cc
ii
ff

结果

当前的Python脚本无法按预期运行

The current Python script does not work as expected

当前

c:\Temp>C:\sort_by_url.py
['gg\nbb\naa\ncc\nii\nff']
<built-in method sort of list object at 0x000000000227BE48>

预期

aa
bb
cc
ff
gg
ii

推荐答案

您正在打印list.sort函数.

尝试

lines.sort()
print lines

list.sort对同一对象执行操作.如果您不想更改原始列表,则可以使用sorted(list)函数创建排序列表.

list.sort perform operation on same object. If you don't want to change original list, then you can create sorted list using sorted(list) function.

这篇关于无法排序多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆