简体   繁体   English

如何使用 IO.stringIO() 中的 readlines 在 zenpython 中仅打印 5 行

[英]How to print just 5 lines in zenpython using readlines from IO.stringIO()

import io

def main():
    zenPython = '''
    The Zen of Python, by Tim Peters

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!
    '''
    fp = io.StringIO(zenPython)

    #Add Implementation step here
    li=fp.readlines()

how to print just 5 lines of zenpython.如何仅打印 5 行 zenpython。 i have tried to pass argument 5 in readlines but its not working.我试图在 readlines 中传递参数 5,但它不起作用。 if i use readlines() i will get the output as below.如果我使用 readlines() 我将得到如下输出。 ['\\n', ' The Zen of Python, by Tim Peters\\n', ' \\n', ' Beautiful is better than ugly.\\n', ' Explicit is better than implicit.\\n']..... ['\\n', ' Python 之禅, by Tim Peters\\n', ' \\n', ' 美胜于丑。\\n', ' 显式胜于隐式。\\n'].... .

but i need only 5 line!但我只需要 5 行!

Use fp.readlines()[:5] .使用fp.readlines()[:5]

readlines returns a list. readlines返回一个列表。

This is a answer for your exam question.这是您考试问题的答案。

fp = io.StringIO(zenPython) // assign teh string to fp variable
zenlines=fp.readlines()[:5] //only 5 lines of the string is read from the variable
print(zenlines) //print the output
return(zenlines) 
fp = io.StringIO(zenPython)
lines = []
for each in fp.readlines():
       lines.append(each)
return lines[0:5]

The Zen of Python is available as a python builtin module, called this . Python 之 Zen 可用作 Python 内置模块,称为this When imported, it writes the poem to stdout .导入时,它将诗歌写入stdout You can capture stdout to a StringIO variable, then print only the first 5 lines.您可以将stdout捕获到StringIO变量,然后仅打印前 5 行。 The following works on python3:以下适用于python3:

import contextlib
from io import StringIO
zen = StringIO()

with contextlib.redirect_stdout(zen):
    import this

for i, line in enumerate(zen.getvalue().split('\n')):
    if i < 5:
      print(line)

try this试试这个

li=fp.readlines(100)
print(li)
return(li)

while this is an unorthodox way that prints first 100 bytes of your data which in your case is first 5 lines and that will pass your testcases.虽然这是一种非正统的方式,它打印数据的前 100 个字节,在您的情况下是前 5 行,它将通过您的测试用例。

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

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