您如何在Python中读取文本文件的特定行? [英] How do you read a specific line of a text file in Python?

查看:1519
本文介绍了您如何在Python中读取文本文件的特定行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用Python读取文本文件的整个特定行.我目前有这个:

I'm having trouble reading an entire specific line of a text file using Python. I currently have this:

load_profile = open('users/file.txt', "r")
read_it = load_profile.readline(1)
print read_it

当然,这只会读取第一行的一个字节,这不是我想要的.我也尝试过Google,但没有找到任何东西.

Of course this will just read one byte of the first line, which is not what I want. I also tried Google but didn't find anything.

推荐答案

此行的条件是什么?是在某个指标上吗?它是否包含某个字符串?它与正则表达式匹配吗?

What are the conditions of this line? Is it at a certain index? Does it contain a certain string? Does it match a regex?

此代码将基于字符串匹配文件中的一行:

This code will match a single line from the file based on a string:

load_profile = open('users/file.txt', "r")
read_it = load_profile.read()
myLine = ""
for line in read_it.splitlines():
    if line == "This is the line I am looking for":
        myLine = line
        break
print myLine

这将为您提供文件的第一行(还有其他几种方法可以做到这一点):

And this will give you the first line of the file (there are several other ways to do this as well):

load_profile = open('users/file.txt', "r")
read_it = load_profile.read().splitlines()[0]
print read_it

或者:

load_profile = open('users/file.txt', "r")
read_it = load_profile.readline()
print read_it


签出 Python文件对象文档

file.readline([size])

从文件中读取整行.尾随 换行符保留在字符串中(但在文件中可能不存在换行符) 以不完整的行结尾). [6]如果存在size参数,则 非负数,它是最大字节数(包括结尾) 换行),并且可能会返回不完整的行.如果大小不为0, 仅当立即遇到EOF时,才返回一个空字符串.

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.

注意与stdio的fgets()不同,返回的字符串包含null 字符('\ 0'),如果它们出现在输入中.

Note Unlike stdio‘s fgets(), the returned string contains null characters ('\0') if they occurred in the input.

file.readlines([sizehint])

使用readline()读取直到EOF,然后返回 包含因此读取的行的列表.如果可选的sizehint 存在论点,而不是读到EOF,而是整行 总计约sizehint字节(可能在舍入为 内部缓冲区大小).实现类似文件的对象 如果无法实现,则界面可以选择忽略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.


回答您的评论诺亚:


Answer to your comment Noah:

load_profile = open('users/file.txt', "r")
read_it = load_profile.read()
myLines = []
for line in read_it.splitlines():
    # if line.startswith("Start of line..."):
    # if line.endswith("...line End."):
    # if line.find("SUBSTRING") > -1:
    if line == "This is the line I am looking for":
        myLines.append(line)
print myLines

这篇关于您如何在Python中读取文本文件的特定行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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