简体   繁体   English

将文件中的数据添加到列表以进行排序

[英]Adding data from a file to a list for sorting

my text file contains some integer values like below: 我的文本文件包含一些整数值,如下所示:

100
200
300
400
500
600
700
800
900
1000

i want to add them to a list, then sort them and print the sorted list. 我想将它们添加到列表中,然后对其进行排序并打印排序后的列表。 i tried something like: 我尝试过类似的东西:

file = open("sample.txt","r")
list1 = []

for line in range(1,11):
    data = file.readline(line)
    list1.append(data)

sortedlist = list1.sort(key=int)
print sortedlist
file.close()

but i got an error like: 但是我遇到了一个错误:

Traceback (most recent call last):
  File "o_2.py", line 8, in <module>
    sortedlist = list1.sort(key=int)
ValueError: invalid literal for int() with base 10: ''

also when i checked the list without sorting it was something like: 另外,当我检查列表而不排序时,它是这样的:

['1', '00', '\n', '200\n', '300\n', '400\n', '500\n', '600\n', '700\n', '800\n']

i am actually very new to python. 我实际上对python很新。 so if anybody could explain the whole case with solution it would be very helpful for me. 因此,如果有人可以用解决方案解释整个案例,那对我会非常有帮助。 thanks in advance. 提前致谢。

You are reading the list wrong; 您看错了清单; readline does not take the line number, but the maximum number of characters per line : readline不使用行号,而是每行最大字符数

file.readline([size])

Read one entire line from the file. 从文件中读取整行。 A trailing newline character is kept in the string (but may be absent when a file ends with an incomplete line). 字符串中保留尾随换行符(但是,如果文件以不完整的行结尾,则可能不存在)。 [6] If the size argument is present and non-negative, it is a maximum byte count (including the trailing newline) and an incomplete line may be returned. [6] 如果size参数存在且为非负数,则为最大字节数(包括结尾的换行符),并且可能返回不完整的行。 When size is not 0, an empty string is returned only when EOF is encountered immediately. 如果size不为0,则仅当立即遇到EOF时才返回空字符串。

Your code would work if you just did file.readline() : 如果您只是执行file.readline()则您的代码将起作用:

for line in range(1,11):
    data = file.readline()
    list1.append(data)

The current code tries to read only 1, 2 and 3 characters of the first line, which results in '1' , '00' and the '\\n' newline being read separately, followed by max 4 characters of line 2 ( '200\\n' ) etc. 当前代码尝试仅读取第一行的1、2和3个字符,这导致分别读取'1''00''\\n'换行符,然后读取第2行(最多'200\\n'个字符) '200\\n' )等。


However it is not very pythonic either; 但是,它也不是很pythonic。 I would write it as: 我将其写为:

with open("sample.txt") as file:
     list1 = [ int(line) for line in file ]

sortedlist = list1.sort()
print sortedlist

with automatically closes the file at the end of indented block. with在缩进块的末尾自动关闭文件。 for loop for a file automatically iterates over its lines; 文件的for循环自动遍历其行; [ expression for var in iterable ] is a list comprehension, that is a shorter way of doing: [ expression for var in iterable ]是列表理解,这是一种较短的方法:

result = []
for var in iterable:
    result.append(expression)

Or as Jon Clements suggested, if you really want to sort the lines by their numerical value, but keeping them as string: 或如乔恩·克莱门茨(Jon Clements)所建议的那样,如果您真的想按数值对线进行排序,但将其保留为字符串:

with open('sample.txt') as file:
    list1 = list(file)  # all lines as a list
    print sorted(list1, key=int)

Your code not removes spaces and newlines. 您的代码不会删除空格和换行符。

Import re txt=open('file').read() List=Re.findall('\\d ',txt) now list contain all numbers all other symbols will skipped 导入re txt = open('file')。read()List = Re.findall('\\ d',txt)现在列表包含所有数字,所有其他符号将被跳过

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM