當前位置:首頁 » 編程語言 » 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是當前游標所在列。我們如果想得到文件代碼有多少行,我們可以直接移動游標到行末,以此來得到一個行數。

熱點內容
Uc瀏覽器上傳 發布:2025-01-09 01:50:16 瀏覽:527
javamysqljar 發布:2025-01-09 01:50:14 瀏覽:771
屏幕設置密碼怎麼設置 發布:2025-01-09 01:48:01 瀏覽:344
快吧我的世界盒子伺服器連接不上 發布:2025-01-09 01:36:11 瀏覽:381
搭建中轉雲伺服器挖礦 發布:2025-01-09 01:27:12 瀏覽:551
存儲過程中的for循環 發布:2025-01-09 01:25:38 瀏覽:862
阿里雲伺服器寬頻是專線嗎 發布:2025-01-09 01:22:42 瀏覽:605
上門修個密碼箱鎖多少錢 發布:2025-01-09 01:21:49 瀏覽:267
python企業培訓 發布:2025-01-09 01:17:14 瀏覽:893
怎樣存儲氣體 發布:2025-01-09 01:17:13 瀏覽:261