python从文件中读取单行作为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16473965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python read single line from file as string
提问by user1895629
I have a text file with a bunch of numbers then another line of numbers and another and another etc. with n number of lines
我有一个文本文件,里面有一堆数字,然后是另一行数字,还有另一个等等,有 n 行
how can I read it and store the lines into n number of strings?
我怎样才能读取它并将这些行存储到 n 个字符串中?
采纳答案by pradyunsg
From the docs.
从文档。
Or specifically:
或者具体来说:
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. When size is not 0, an empty string is returned only when EOF is encountered immediately.
file.readlines([sizehint])Read until EOF using readline() and return a list containing the lines thus read. If the optional sizehint argument is present, instead of reading up to EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an internal buffer size) are read. Objects implementing a file-like interface may choose to ignore sizehint if it cannot be implemented, or cannot be implemented efficiently.
file.readline([size])从文件中读取一整行。尾随换行符保留在字符串中(但当文件以不完整的行结束时可能不存在)。[6]如果 size 参数存在且非负,则它是最大字节数(包括尾随换行符)并且可能返回不完整的行。当 size 不为 0 时,仅当立即遇到 EOF 时才返回空字符串。
file.readlines([sizehint])使用 readline() 读取直到 EOF 并返回包含由此读取的行的列表。如果存在可选的 sizehint 参数,而不是读取到 EOF,而是读取总计大约 sizehint 字节(可能在向上舍入到内部缓冲区大小之后)的整行。实现类文件接口的对象可能会选择忽略 sizehint,如果它不能被实现,或者不能被有效地实现。
回答by Ben Schwabe
I don't think you did much research before this, but I'll try to help out. It sounds like, generally, what you want is f.readlines(). Specifically, check out this python documentationon how to do it. Your code to open and display a file line by line would look something like this, however:
我认为您在此之前没有进行太多研究,但我会尽力提供帮助。听起来,一般来说,你想要的是f.readlines(). 具体来说,请查看有关如何执行此操作的 Python 文档。您用于逐行打开和显示文件的代码如下所示:
f = open("file.txt","r")
for line in f:
print line
Alternatively, you could write to a list and then call it later:
或者,您可以写入一个列表,然后稍后调用它:
#STEP 1
f = open("file.txt","r")
#STEP 2
linelist = f.readlines()
count = len(linelist)
#STEP 3
print count
input = input("display line number:")
print lineList[input]
What this second code does, is as follows.
第二个代码的作用如下。
- It opens the file for reading, and initiates a few variables to be used.
- It runs through each line of your document and adds it to the list
lineList - After it runs through every line in your document, it displays the number of lines total and asks you to pick a line to display. Then the code ends.
- 它打开文件进行读取,并启动一些要使用的变量。
- 它贯穿文档的每一行并将其添加到列表中
lineList - 在遍历文档中的每一行后,它会显示总行数并要求您选择要显示的行。然后代码结束。

