Thực hiện đơn giản lệnh đuôi trong Python Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều
| '' '
|
| Thực hiện lệnh đuôi cơ bản
|
| Usage:
|
|
|
| Đuôi.py FileName Numlines
|
|
|
| '' '
|
|
|
| Thực hiện lệnh đuôi cơ bản sys
|
| Đuôi.py FileName Numlines linecache
|
|
|
| Nhập khẩu len(sys.argv) !=3:
|
| Nhập khẩu LineCache 'Usage: tail.py '
|
| iflen (sys.argv)! = 3:.exit(1)
|
|
|
| Print'Usage: Tail.py '
|
| sys.exit (1), nlines = sys.argv[1:]
|
| # Tên tệp và số dòng được yêu cầu = int(nlines)
|
|
|
| fname, nlines = sys.argv [1:]
|
| nlines = int (nlines) = len(open(fname).readlines())
|
|
|
| # Đếm tổng số dòng
|
| tot_lines = len (mở (fname) .ReadLines ()) i in range(tot_lines - nlines + 1, tot_lines+1):
|
| # Sử dụng mô -đun bộ đệm dòng để đọc các dòng linecache.getline(sys.argv[1],i),
|
Tệp này chứa văn bản unicode hai chiều có thể được giải thích hoặc biên dịch khác với những gì xuất hiện dưới đây. Để xem xét, hãy mở tệp trong một trình soạn thảo cho thấy các ký tự Unicode ẩn. Tìm hiểu thêm về các ký tự unicode hai chiều
| foriinrange (tot_lines-nlines+1, tot_lines+1):
|
| printlinecache.getline (sys.argv [1], i),
|
| "" "Đây là phiên bản hiệu quả hơn, vì nó không đọc toàn bộ
|
|
|
| Thực hiện lệnh đuôi cơ bản sys
|
| Đuôi.py FileName Numlines os
|
|
|
| bufsize=8192 = 8192
|
|
|
| Nhập khẩu = int(sys.argv[1])
|
| Nhập khẩu LineCache = sys.argv[2]
|
| fsize=os.stat(fname).st_size = os.stat(fname).st_size
|
|
|
| iter=0 = 0
|
| iflen (sys.argv)! = 3: open(sys.argv[2]) as f:
|
| Print'Usage: Tail.py ' bufsize > fsize:
|
| bufsize=fsize-1 = fsize-1
|
| sys.exit (1) = []
|
| # Tên tệp và số dòng được yêu cầu True:
|
| iter+=1 +=1
|
| fname, nlines = sys.argv [1:].seek(fsize-bufsize*iter)
|
| nlines = int (nlines).extend(f.readlines())
|
| # Đếm tổng số dòng len(data) >= lines or f.tell() == 0:
|
| tot_lines = len (mở (fname) .ReadLines ())(''.join(data[-lines:]))
|
| break
| Cách xem pythonic để xem phần cuối của một tệp đang phát triển cho sự xuất hiện của một số từ khóa nhất định là gì? Trong vỏ tôi có thể nói: tail -f "$file" | grep "$string" | while read hit; do
#stuff
done
DBR 162K65 Huy hiệu vàng273 Huy hiệu bạc340 Huy hiệu Đồng65 gold badges273 silver badges340 bronze badges Đã hỏi ngày 9 tháng 11 năm 2009 lúc 20:40Nov 9, 2009 at 20:40
2 Chà, cách đơn giản nhất sẽ liên tục đọc từ tệp, kiểm tra những gì mới và kiểm tra cho các lượt truy cập. import time
def watch(fn, words):
fp = open(fn, 'r')
while True:
new = fp.readline()
# Once all lines are read this just returns ''
# until the file changes and a new line appears
if new:
for word in words:
if word in new:
yield (word, new)
else:
time.sleep(0.5)
fn = 'test.py'
words = ['word']
for hit_word, hit_sentence in watch(fn, words):
print "Found %r in line: %r" % (hit_word, hit_sentence)
Giải pháp này với readline hoạt động nếu bạn biết dữ liệu của bạn sẽ xuất hiện trong các dòng. Nếu dữ liệu là một loại luồng bạn cần một bộ đệm, lớn hơn word lớn nhất mà bạn đang tìm kiếm và điền vào nó trước. Nó trở nên phức tạp hơn một chút theo cách đó ...
DBR 162K65 Huy hiệu vàng273 Huy hiệu bạc340 Huy hiệu Đồng65 gold badges273 silver badges340 bronze badges Đã hỏi ngày 9 tháng 11 năm 2009 lúc 20:40Nov 9, 2009 at 20:49 Chà, cách đơn giản nhất sẽ liên tục đọc từ tệp, kiểm tra những gì mới và kiểm tra cho các lượt truy cập.Jochen Ritzel Giải pháp này với readline hoạt động nếu bạn biết dữ liệu của bạn sẽ xuất hiện trong các dòng.29 gold badges196 silver badges191 bronze badges 2 def tail(f):
f.seek(0, 2)
while True:
line = f.readline()
if not line:
time.sleep(0.1)
continue
yield line
def process_matches(matchtext):
while True:
line = (yield)
if matchtext in line:
do_something_useful() # email alert, etc.
list_of_matches = ['ERROR', 'CRITICAL']
matches = [process_matches(string_match) for string_match in list_of_matches]
for m in matches: # prime matches
m.next()
while True:
auditlog = tail( open(log_file_to_monitor) )
for line in auditlog:
for m in matches:
m.send(line)
Nếu dữ liệu là một loại luồng bạn cần một bộ đệm, lớn hơn word lớn nhất mà bạn đang tìm kiếm và điền vào nó trước. Nó trở nên phức tạp hơn một chút theo cách đó ... Đã trả lời ngày 9 tháng 11 năm 2009 lúc 20:49Nov 9, 2009 at 21:33 0 Jochen Ritzeljochen Ritzel def tail(filename, bufsize = 1024):
fds = [ os.open(filename, os.O_RDONLY) ]
while True:
reads, _, _ = select.select(fds, [], [])
if 0 < len(reads):
yield os.read(reads[0], bufsize)
Đã trả lời ngày 9 tháng 11 năm 2009 lúc 20:49Nov 9, 2009 at 21:33 1 Jochen Ritzeljochen Ritzeldoesn't answer the actual question that
was asked. Original answer remains below for posterity. (Calling out to tail and grep will work, but is a non-answer of sorts anyway.) 102K29 Huy hiệu vàng196 Huy hiệu bạc191 Huy hiệu Đồng Tôi sử dụng điều này để theo dõi các tệp nhật ký. Trong quá trình triển khai đầy đủ, tôi giữ list_of_matches trong tệp cấu hình để nó có thể được sử dụng cho nhiều mục đích. Trong danh sách các cải tiến của tôi là hỗ trợ cho Regex thay vì trận đấu 'trong' đơn giản.Nov 9, 2009 at 20:55 Đã trả lời ngày 9 tháng 11 năm 2009 lúc 21:33Walter Mundt Bạn có thể sử dụng Chọn để thăm dò ý kiến cho nội dung mới trong một tệp.5 gold badges51 silver badges60 bronze badges 2 Chỉnh sửa: Như nhận xét dưới đây ghi chú, import time
def watch(fn, words):
fp = open(fn, 'r')
while True:
new = fp.readline()
# Once all lines are read this just returns ''
# until the file changes and a new line appears
if new:
for word in words:
if word in new:
yield (word, new)
else:
time.sleep(0.5)
fn = 'test.py'
words = ['word']
for hit_word, hit_sentence in watch(fn, words):
print "Found %r in line: %r" % (hit_word, hit_sentence)
0 không hoạt động cho các tệp trên đĩa. Điều này vẫn sẽ giúp nếu bất cứ ai khác đi cùng tìm đến dữ liệu đến từ ổ cắm hoặc đường ống được đặt tên hoặc quy trình khác, nhưng nó không trả lời câu hỏi thực tế được hỏi. Câu trả lời ban đầu vẫn còn dưới đây cho hậu thế. (Gọi Tail và Grep sẽ hoạt động, nhưng dù sao cũng không phải là người trả lời.)from tailf import tailf
for line in tailf("myfile.log"):
print line
Mở tệp bằng import time
def watch(fn, words):
fp = open(fn, 'r')
while True:
new = fp.readline()
# Once all lines are read this just returns ''
# until the file changes and a new line appears
if new:
for word in words:
if word in new:
yield (word, new)
else:
time.sleep(0.5)
fn = 'test.py'
words = ['word']
for hit_word, hit_sentence in watch(fn, words):
print "Found %r in line: %r" % (hit_word, hit_sentence)
0 và sử dụng import time
def watch(fn, words):
fp = open(fn, 'r')
while True:
new = fp.readline()
# Once all lines are read this just returns ''
# until the file changes and a new line appears
if new:
for word in words:
if word in new:
yield (word, new)
else:
time.sleep(0.5)
fn = 'test.py'
words = ['word']
for hit_word, hit_sentence in watch(fn, words):
print "Found %r in line: %r" % (hit_word, hit_sentence)
2 để thăm dò ý kiến đọc và sau đó import time
def watch(fn, words):
fp = open(fn, 'r')
while True:
new = fp.readline()
# Once all lines are read this just returns ''
# until the file changes and a new line appears
if new:
for word in words:
if word in new:
yield (word, new)
else:
time.sleep(0.5)
fn = 'test.py'
words = ['word']
for hit_word, hit_sentence in watch(fn, words):
print "Found %r in line: %r" % (hit_word, hit_sentence)
3 để đọc dữ liệu mới và các phương thức chuỗi để lọc các dòng ở cuối tệp ... hoặc chỉ sử dụng mô -đun import time
def watch(fn, words):
fp = open(fn, 'r')
while True:
new = fp.readline()
# Once all lines are read this just returns ''
# until the file changes and a new line appears
if new:
for word in words:
if word in new:
yield (word, new)
else:
time.sleep(0.5)
fn = 'test.py'
words = ['word']
for hit_word, hit_sentence in watch(fn, words):
print "Found %r in line: %r" % (hit_word, hit_sentence)
4 và để import time
def watch(fn, words):
fp = open(fn, 'r')
while True:
new = fp.readline()
# Once all lines are read this just returns ''
# until the file changes and a new line appears
if new:
for word in words:
if word in new:
yield (word, new)
else:
time.sleep(0.5)
fn = 'test.py'
words = ['word']
for hit_word, hit_sentence in watch(fn, words):
print "Found %r in line: %r" % (hit_word, hit_sentence)
5 và import time
def watch(fn, words):
fp = open(fn, 'r')
while True:
new = fp.readline()
# Once all lines are read this just returns ''
# until the file changes and a new line appears
if new:
for word in words:
if word in new:
yield (word, new)
else:
time.sleep(0.5)
fn = 'test.py'
words = ['word']
for hit_word, hit_sentence in watch(fn, words):
print "Found %r in line: %r" % (hit_word, hit_sentence)
6 công việc cho bạn giống như bạn làm trong vỏ.Mar 16, 2015 at 11:02 Đã trả lời ngày 9 tháng 11 năm 2009 lúc 20:55kommradHomer Walter Mundtwalter Mundt4 gold badges50 silver badges67
bronze badges 2 24.2K5 Huy hiệu vàng51 Huy hiệu bạc60 Huy hiệu Đồng Bạn có thể sử dụng Pytailf: Đuôi Python đơn giản -F Đã trả lời ngày 16 tháng 3 năm 2015 lúc 11:02Kommradhomerkommradhomer 4.0314 Huy hiệu vàng50 Huy hiệu bạc67 Huy hiệu ĐồngNov 9, 2009 at 20:54 Nếu bạn không thể hạn chế vấn đề để làm việc cho một lần đọc dựa trên dòng, bạn cần phải dùng đến các khối.deets Điều này sẽ hoạt động:27 silver badges27 bronze badges 1 import sys
needle = "needle"
blocks = []
inf = sys.stdin
if len(sys.argv) == 2:
inf = open(sys.argv[1])
while True:
block = inf.read()
blocks.append(block)
if len(blocks) >= 2:
data = "".join((blocks[-2], blocks[-1]))
else:
data = blocks[-1]
# attention, this needs to be changed if you are interested
# in *all* matches separately, not if there was any match ata all
if needle in data:
print "found"
blocks = []
blocks[:-2] = []
if block == "":
break
Thách thức nằm ở việc đảm bảo bạn khớp với kim ngay cả khi nó bị ngăn cách bởi hai giới hạn khối. Đã trả lời ngày 9 tháng 11 năm 2009 lúc 20:54Nov 9, 2009 at 20:51 Deetsdeets import subprocess
def tailf(filename):
#returns lines from a file, starting from the beginning
command = "tail -n +1 -F " + filename
p = subprocess.Popen(command.split(), stdout=subprocess.PIPE, universal_newlines=True)
for line in p.stdout:
yield line
for line in tailf("logfile"):
#do stuff
Nó chặn chờ các dòng mới được viết, vì vậy điều này không phù hợp để sử dụng không đồng bộ mà không cần sửa đổi. Đã trả lời ngày 28 tháng 6 năm 2017 lúc 8:08Jun 28, 2017 at 8:08
JamesjamesJames 1.2431 Huy hiệu vàng11 Huy hiệu bạc17 Huy hiệu đồng1 gold
badge11 silver badges17 bronze badges 1 Bạn có thể sử dụng import time
def watch(fn, words):
fp = open(fn, 'r')
while True:
new = fp.readline()
# Once all lines are read this just returns ''
# until the file changes and a new line appears
if new:
for word in words:
if word in new:
yield (word, new)
else:
time.sleep(0.5)
fn = 'test.py'
words = ['word']
for hit_word, hit_sentence in watch(fn, words):
print "Found %r in line: %r" % (hit_word, hit_sentence)
7 để thực hiện đuôi.Từ http://docs.python.org/l Library/collections.html#deque-cipes ... def tail(filename, n=10):
'Return the last n lines of a file'
return deque(open(filename), n)
Tất nhiên, điều này đọc toàn bộ nội dung tệp, nhưng đó là một cách thực hiện đuôi gọn gàng và ngắn gọn. Đã trả lời ngày 9 tháng 11 năm 2009 lúc 21:17Nov 9, 2009 at 21:17 FoglebirdfoglebirdFogleBird 71.8K25 Huy hiệu vàng121 Huy hiệu bạc131 Huy hiệu đồng25 gold badges121 silver badges131 bronze
badges 1
Làm thế nào để bạn viết các lệnh đuôi trong Python?
Hàm dataFrame - đuôi () hàm đuôi () được sử dụng để có được n hàng cuối cùng. Hàm này trả về n hàng cuối cùng từ đối tượng dựa trên vị trí. Nó rất hữu ích để nhanh chóng xác minh dữ liệu, ví dụ, sau khi sắp xếp hoặc nối các hàng. Số lượng hàng để chọn.The tail() function is used to get the last n rows. This function returns last n rows from the object based on position. It is useful for quickly verifying data, for example, after sorting or appending rows. Number of rows to select.
Lệnh đuôi trong Python là gì?
Hàm đuôi trong Python hiển thị năm hàng cuối cùng của DataFrame theo mặc định. Nó có trong một tham số duy nhất: số lượng hàng. Chúng ta có thể sử dụng tham số này để hiển thị số lượng hàng mà chúng ta chọn.displays the last five rows of the dataframe by default. It takes in a single parameter: the number of rows. We can use this parameter to display the number of rows of our choice.
Làm thế nào để bạn sử dụng lệnh đuôi?
Lệnh đuôi cũng đi kèm với tùy chọn '+' không có trong lệnh đầu.Với lệnh Tùy chọn này, lệnh đuôi in dữ liệu bắt đầu từ số dòng được chỉ định của tệp thay vì kết thúc.Đối với lệnh: đuôi +n file_name, dữ liệu sẽ bắt đầu in từ số dòng 'n' cho đến khi kết thúc tệp được chỉ định.tail +n file_name, data will start printing from line number 'n' till the end of the file specified.
Đuôi là gì trong lệnh là gì
Lệnh đuôi là một tiện ích dòng lệnh để xuất phần cuối cùng của các tệp được cung cấp thông qua đầu vào tiêu chuẩn.Nó viết kết quả cho đầu ra tiêu chuẩn.Theo mặc định, đuôi trả về mười dòng cuối cùng của mỗi tệp mà nó được đưa ra.Nó cũng có thể được sử dụng để theo một tệp trong thời gian thực và xem khi các dòng mới được viết cho nó.a command-line utility for outputting the last part of files given to it via standard input. It writes results to standard output. By default tail returns the last ten lines of each file that it is given. It may also be used to follow a file in real-time and watch as new lines are written to it. |