当前位置:首页 » 编程语言 » python行号

python行号

发布时间: 2022-02-04 17:50:44

python中怎么打印行号和文件名

importfileinput
importglob
importstring,sys

forlineinfileinput.input(glob.glob("samples/*.txt")):
iffileinput.isfirstline():#firstinafile?
sys.stderr.write("--reading%s-- "%fileinput.filename())
sys.stdout.write(str(fileinput.lineno())+""+string.upper(line))

用这个

㈡ python中line对象的行号怎么获得

python中line对象的行号怎么获得
#-*- coding: utf-8 -*-import res = '''1 #!/usr/bin/env python23 from cgi import FieldStorage4 from os import environ5 from cStringIO import StringIO 6 from urllib import quote, unquote7 from string import capwords, strip, split, join89 class AdvCGI(object):1011 header = 'Content-Type: text/html\n\n'12 url = '/py/advcgi.py'13'''LINE_PATTERN =r'\s*\d+\s?(.*)'def func(text): c = re.compile(LINE_PATTERN) lists = [] lines = text.split('\n') for line in lines: r = c.findall(line) if r: lists.append(r[0]) return '\n'.join(lists)if __name__ == '__main__': l = func(s) print l

㈢ python的IDLE设置行号

配置起来 不好用
还不如换个 编辑器
比如
Sublime Text
PyCharm

㈣ 如何得到python的当前函数名及行号

import fileinputimport globimport string, sys for line in fileinput.input(glob.glob("samples/*.txt")): if fileinput.isfirstline(): # first in a file? sys.stderr.write("-- reading %s --\n" % fileinput.filename()) sys.stdout.write(str(fileinput.lineno()) + " " + string.upper(line)) 用这个

㈤ 在idle中如何显示行号

其实IDLE提供了一个显示所有行和所有字符的功能。

我们打开IDLE shell或者IDLE编辑器,可以看到左下角有个Ln和Col,事实上,Ln是当前光标所在行,Col是当前光标所在列。

我们如果想得到文件代码有多少行,我们可以直接移动光标到行末,以此来得到一个行数。

㈥ 如何用python实现去掉文本中的行序号(行号)

LINE_PATTERN =r'\s*\d+\s?(.*)'能给我详细讲讲这个正则表达式吗? \s 是匹配任何空白字符,*匹配前面的正则出现0次或多次,\d匹配数字,+表示数字出现一次或多次。 我不明白的是,r = c.findall('2 v=[] \n') 后为什么r=['v=[] '],它是如何实现将数字去掉的?

㈦ 关于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编程:怎么为该程序输出的每行标上行号:

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

㈨ IDLE(python) 怎么显示行数

1、打开IDLE shell或者IDLE编辑器,可以看到左下角有个Ln和Col,事实上,Ln是当前光标所在行,Col是当前光标所在列。我们如果想得到文件代码有多少行,我们可以直接移动光标到行末,以此来得到一个行数。

热点内容
同方存储 发布:2025-01-09 17:04:30 浏览:796
网络连接一般什么密码 发布:2025-01-09 17:04:30 浏览:390
脸书的账号密码在哪里 发布:2025-01-09 16:59:16 浏览:191
台湾服务器怎么选云空间 发布:2025-01-09 16:50:06 浏览:440
防走失牵引绳密码如何找回 发布:2025-01-09 16:39:14 浏览:705
压缩机的构造 发布:2025-01-09 16:31:13 浏览:151
安卓iis服务器搭建 发布:2025-01-09 16:31:11 浏览:856
斗地主编程 发布:2025-01-09 16:31:11 浏览:595
我的世界花雨亭服务器怎么玩 发布:2025-01-09 16:31:10 浏览:320
在vmware上安装linux 发布:2025-01-09 16:30:36 浏览:113