在python中获取第n行字符串

时间:2012-07-15 12:20:52

标签: python python-3.x

如何在Python 3中获取字符串的 nth 行? 例如

getline("line1\nline2\nline3",3)

使用stdlib / builtin函数有没有办法做到这一点? 我更喜欢Python 3中的解决方案,但Python 2也很好。

9 个答案:

答案 0 :(得分:19)

尝试以下方法:

s = "line1\nline2\nline3"
print s.splitlines()[2]

答案 1 :(得分:3)

功能性方法

>>> import StringIO
>>> from itertools import islice
>>> s = "line1\nline2\nline3"
>>> gen = StringIO.StringIO(s)
>>> print next(islice(gen, 2, 3))
line3

答案 2 :(得分:2)

使用字符串缓冲区:

import io    
def getLine(data, line_no):
    buffer = io.StringIO(data)
    for i in range(line_no - 1):
        try:
            next(buffer)
        except StopIteration:
            return '' #Reached EOF

    try:
        return next(buffer)
    except StopIteration:
        return '' #Reached EOF

答案 3 :(得分:1)

从评论中看起来好像这个字符串非常大。 如果有太多数据可以轻松适应内存,一种方法是逐行处理文件中的数据:

N = ...
with open('data.txt') as inf:
    for count, line in enumerate(inf, 1):
        if count == N: #search for the N'th line
            print line

使用enumerate()为您提供索引和正在迭代的对象的值,您可以指定起始值,因此我使用1(而不是默认值0)

使用with的好处是,当您完成或遇到异常时,它会自动为您关闭文件。

答案 4 :(得分:1)

比分割字符串更有效的解决方案是遍历其字符,找到第N个位置和第(N-1)个'\ n'出现的位置(考虑到开头处的边缘情况)字符串)。第N行是这些位置之间的子串。

这是一段用于演示它的混乱代码(行号为1索引):

def getLine(data, line_no):
    n = 0
    lastPos = -1
    for i in range(0, len(data) - 1):
        if data[i] == "\n":
            n = n + 1
            if n == line_no:
                return data[lastPos + 1:i]
            else:
                lastPos = i;



    if(n == line_no - 1):
        return data[lastPos + 1:]
    return "" # end of string

这也比一次构建一个字符串的解决方案更有效。

答案 5 :(得分:1)

既然你提高了记忆效率,那就更好了:

s = "line1\nline2\nline3"

# number of the line you want
line_number = 2

i = 0
line = ''
for c in s:
   if i > line_number:
     break
   else:
     if i == line_number-1 and c != '\n':
       line += c
     elif c == '\n':
       i += 1

答案 6 :(得分:0)

为便于阅读而写入了两个功能

    string = "foo\nbar\nbaz\nfubar\nsnafu\n"

    def iterlines(string):
      word = ""
      for letter in string:
        if letter == '\n':
          yield word
          word = ""
          continue
        word += letter

    def getline(string, line_number):
      for index, word in enumerate(iterlines(string),1):
        if index == line_number:
          #print(word)
          return word

    print(getline(string, 4))

答案 7 :(得分:0)

{
    "took": 42,
    "timed_out": false,
    "terminated_early": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 42,
        "max_score": 1.0,
        "hits": [
            {
                "_index": "search",
                "_type": "baseCourse",
                "_id": "1813-67-14-2309",
                "_score": 1.0,
                "_source": {
                    "cityName": "Belo Horizonte",
                    "institutionName": "CENTRO DE EDUCAÇÃO SUPERIOR",
                    "campi": [
                        {
                            "address_zipCode": null,
                            "address_street": "Avenida Contorno, 6.475 - São Pedro",
                            "stateCode": "MG",
                            "id": 33,
                            "campus_name": "Unidade SEDE"
                        }
                    ],
                    "baseCourseName": "ADMINISTRAÇÃO"
                },
                "inner_hits": {
                    "campi": {
                        "hits": {
                            "total": 1,
                            "max_score": null,
                            "hits": [
                                {
                                    "_nested": {
                                        "field": "campi",
                                        "offset": 0
                                    },
                                    "_score": null,
                                    "fields": {
                                        "distanceInMeters": [
                                            3593.558492923913
                                        ]
                                    },
                                    "sort": [
                                        3593.558492923913
                                    ]
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}

答案 8 :(得分:-2)

我的解决方案(高效紧凑):

def getLine(data, line_no):
    index = -1
    for _ in range(line_no):index = data.index('\n',index+1)
    return data[index+1:data.index('\n',index+1)]