㈠ python里面idle怎么显示行号
这个取决于你用的编辑器。用Sublime Text 2,默认显示行号。用ActivePython里自带的PythonWin,需要手动设置:打开View->Options...,Editor选项卡中修改Line Numbers 为20。
㈡ python按照指定字符串打印出这个字符串的后面几行中的特定字符串开头的行。
Python Code:
cfgParser=ConfigParser()
cfgParser.read(r"D:a.txt")#路径修改为你电脑中的即可
cfgParser.get("test","path")
输出:'/test1/test'
㈢ 关于Python中的一段为Python脚本添加行号脚本
C语言有__LINE__来表示源代码的当前行号,经常在记录日志时使用。Python如何获取源代码的当前行号?
The C Language has the __LINE__ macro, which is wildly used in logging, presenting the current line of the source file. And how to get the current line of a Python source file?
exception输出的函数调用栈就是个典型的应用:
A typical example is the output of function call stack when an exception:
python代码
File "D:\workspace\Python\src\lang\lineno.py", line 19, in <mole>
afunc()
File "D:\workspace\Python\src\lang\lineno.py", line 15, in afunc
errmsg = 1/0
ZeroDivisionError: integer division or molo by zero
那么我们就从错误栈的输出入手,traceback模块中:
Now that, Let's begin with the output of an exception call stack, in the traceback mole:
python代码
def print_stack(f=None, limit=None, file=None):
"""Print a stack trace from its invocation point.
The optional 'f' argument can be used to specify an alternate
stack frame at which to start. The optional 'limit' and 'file'
arguments have the same meaning as for print_exception().
"""
if f is None:
try:
raise ZeroDivisionError
except ZeroDivisionError:
f = sys.exc_info()[2].tb_frame.f_back
print_list(extract_stack(f, limit), file)
def print_list(extracted_list, file=None):
"""Print the list of tuples as returned by extract_tb() or
extract_stack() as a formatted stack trace to the given file."""
if file is None:
file = sys.stderr
for filename, lineno, name, line in extracted_list:
_print(file,
' File "%s", line %d, in %s' % (filename,lineno,name))
if line:
_print(file, ' %s' % line.strip())
traceback模块构造一个ZeroDivisionError,并通过sys模块的exc_info()来获取运行时上下文。我们看到,所有的秘密都在tb_frame中,这是函数调用栈中的一个帧。
traceback constructs an ZeroDivisionError, and then call the exc_info() of the sys mole to get runtime context. There, all the secrets hide in the tb_frame, this is a frame of the function call stack.
对,就是这么简单!只要我们能找到调用栈frame对象即可获取到行号!因此,我们可以用同样的方法来达到目的,我们自定义一个lineno函数:
Yes, It's so easy! If only a frame object we get, we can get the line number! So we can have a similar implemetation to get what we want, defining a function named lineno:
python代码
import sys
def lineno():
frame = None
try:
raise ZeroDivisionError
except ZeroDivisionError:
frame = sys.exc_info()[2].tb_frame.f_back
return frame.f_lineno
def afunc():
# if error
print "I have a problem! And here is at Line: %s"%lineno()
是否有更方便的方法获取到frame对象?当然有!
Is there any other way, perhaps more convinient, to get a frame object? Of course YES!
python代码
def afunc():
# if error
print "I have a proble! And here is at Line: %s"%sys._getframe().f_lineno
类似地,通过frame对象,我们还可以获取到当前文件、当前函数等信息,就像C语音的__FILE__与__FUNCTION__一样。其实现方式,留给你们自己去发现。
Thanks to the frame object, similarly, we can also get current file and current function name, just like the __FILE__ and __FUNCTION__ macros in C. Debug the frame object, you will get the solutions.
㈣ python怎么PRINT出第几行
a =['a', 'b', 'c']
for i,t in enumerate(a):
print i,t
0 a
1 b
2 c
㈤ python中怎么打印行号和文件名
通过调用堆栈里面的代码对象来获取。
一般是自己引发一个异常,然后捕获这个异常,在异常处理中通过堆栈信息获得行号。
比如:
try:
raiseZeroDivisionError
exceptZeroDivisionError:
frame=sys.exc_info()[2].tb_frame.f_back
returnframe.f_lineno
如果需要更多的信息,可以通过inspect模块的stack()方法获得整个调用栈。可自定义一个exception类型。
importinspect
classMyErrorCode(object):
STANDARD=0x0001
code_map_msg={
MyErrorCode.STANDARD:"standarderror",
}
classMyError(Exception):
def__init__(self,error_code):
self.error_code=error_code
try:
_current_call=inspect.stack()[1]
_iframe=_current_call[0]
self.line_no=_iframe.f_lineno
self.mole_name=_iframe.f_globals.get("__name__","")
self.method_name=_current_call[3]
self.class_name=_iframe.f_locals.get("self",None).__class__.__name__
except(IndexError,AttributeError):
self.line_no=""
self.mole_name=""
self.method_name=""
self.class_name=""
def__repr__(self):
msg=code_map_msg.get(self.error_code,"")
return"[*]MyError:%s>%s.mole:%s,class:%s,method:%s,line:%s"%(self.error_code,msg,self.mole_name,self.class_name,self.method_name,self.line_no)
def__str__(self):
returncode_map_msg.get(self.error_code,"notfindanymatchmsgforcode:%s"%self.error_code)
然后在需要获取行号的地方引发这个异常并捕获它,然后从异常对象中获取line_no.
㈥ python如何打印关键词所在行
forlinein______:#逐行扫描
ifccinline:#检查关键词是否存在
print(line)
break#打印cc出第一次出现的那行文字,结束当前的循环
else:continue
㈦ 知道一个文本中某些行的行号,怎样用python直接读出来呢如只想读出第100行的文本。
1.python中只有seek能跳跃的读,但是是按照字节来的,如果你的文本每一行都是一样的长度的话倒是可以。f.seek(99*n)之后再f.readline()
2.如果不知道每行长度的话,那么就循环100次readline()吧,这个总比直接readlines()好,如果全部长1万行,这样也只读了100行,readlines()却要读10000行。
3.如果文本是自己写的话,可以事先坐下标记最好了。
㈧ 在idle中如何显示行号
其实IDLE提供了一个显示所有行和所有字符的功能。
我们打开IDLE shell或者IDLE编辑器,可以看到左下角有个Ln和Col,事实上,Ln是当前光标所在行,Col是当前光标所在列。
我们如果想得到文件代码有多少行,我们可以直接移动光标到行末,以此来得到一个行数。
㈨ IDLE(python) 怎么显示行数
1、打开IDLE shell或者IDLE编辑器,可以看到左下角有个Ln和Col,事实上,Ln是当前光标所在行,Col是当前光标所在列。我们如果想得到文件代码有多少行,我们可以直接移动光标到行末,以此来得到一个行数。
㈩ 请教大家,python编程:怎么为该程序输出的每行标上行号:
count = 1
for i in range(1,5):
....for j in range(1,5):
........for k in range(1,5):
............if( i != k ) and (i != j) and (j != k):
................print count,':',i,j,k
............count+=1