Trong Python, có thể cắt ra một phần văn bản trong một tài liệu khi bạn chỉ biết từ đầu và từ kết thúc? Ví dụ: sử dụng Bill of Rights làm tài liệu mẫu, tìm kiếm "Sửa đổi 3" và xóa tất cả văn bản cho đến khi bạn nhấn "Sửa đổi 4" mà không thực sự biết hoặc quan tâm đến văn bản nào tồn tại giữa hai điểm cuối. Lý do tôi yêu cầu là tôi muốn sử dụng tập lệnh Python này để sửa đổi các chương trình Python khác của tôi khi tôi tải chúng lên máy tính của khách hàng-xóa các phần mã tồn tại giữa một bình luận có nội dung "#Chop-Begin" và " #end-end ". Tôi không muốn khách hàng có quyền truy cập vào tất cả các chức năng mà không trả tiền cho phiên bản tốt hơn của mã. Đã hỏi ngày 21 tháng 2 năm 2011 lúc 22:12Feb 21, 2011 at 22:12 Thuyền trưởngandCokeCaptainandCokecaptainandcoke 1.0652 Huy hiệu vàng13 Huy hiệu bạc16 Huy hiệu đồng2 gold badges13 silver badges16 bronze badges Bạn có thể sử dụng mô -đun do_something_public()
#chop-begin abcd
get_rid_of_me() #chop-end
#chop-beginner this should stay!
#chop-begin
do_something_private()
#chop-end The rest of this comment should go too!
but_you_need_me() #chop-begin
last_to_go()
#chop-end
1 của Python.Tôi đã viết tập lệnh ví dụ này để xóa các phần của mã trong tệp: import re
# Create regular expression pattern
chop = re.compile('#chop-begin.*?#chop-end', re.DOTALL)
# Open file
f = open('data', 'r')
data = f.read()
f.close()
# Chop text between #chop-begin and #chop-end
data_chopped = chop.sub('', data)
# Save result
f = open('data', 'w')
f.write(data_chopped)
f.close()
Đã trả lời ngày 21 tháng 2 năm 2011 lúc 22:25Feb 21, 2011 at 22:25 Viktor Stískalaviktor StískalaViktor Stískala 1.4271 Huy hiệu vàng13 Huy hiệu bạc23 Huy hiệu đồng1 gold badge13 silver badges23 bronze badges 2 Với data.txt do_something_public()
#chop-begin abcd
get_rid_of_me() #chop-end
#chop-beginner this should stay!
#chop-begin
do_something_private()
#chop-end The rest of this comment should go too!
but_you_need_me() #chop-begin
last_to_go()
#chop-end
mã sau import re
class Chopper(object):
def __init__(self, start='\\s*#ch'+'op-begin\\b', end='#ch'+'op-end\\b.*?$'):
super(Chopper,self).__init__()
self.re = re.compile('{0}.*?{1}'.format(start,end), flags=re.DOTALL+re.MULTILINE)
def chop(self, s):
return self.re.sub('', s)
def chopFile(self, infname, outfname=None):
if outfname is None:
outfname = infname
with open(infname) as inf:
data = inf.read()
with open(outfname, 'w') as outf:
outf.write(self.chop(data))
ch = Chopper()
ch.chopFile('data.txt')
kết quả trong dữ liệu.txt do_something_public()
#chop-beginner this should stay!
but_you_need_me()
Đã trả lời ngày 21 tháng 2 năm 2011 lúc 22:22Feb 21, 2011 at 22:22 Hugh Bothwellhugh BothwellHugh Bothwell 54.1K7 Huy hiệu vàng82 Huy hiệu bạc98 Huy hiệu Đồng7 gold badges82 silver
badges98 bronze badges 3 Sử dụng các biểu thức thông thường: import re
string = re.sub('#chop-begin.*?#chop-end', '', string, flags=re.DOTALL)
do_something_public()
#chop-begin abcd
get_rid_of_me() #chop-end
#chop-beginner this should stay!
#chop-begin
do_something_private()
#chop-end The rest of this comment should go too!
but_you_need_me() #chop-begin
last_to_go()
#chop-end
2 sẽ phù hợp với tất cả giữa. Đã trả lời ngày 21 tháng 2 năm 2011 lúc 22:20Feb 21, 2011 at 22:20
2 Nội dung - Giới thiệu
- Ví dụ 1: Thay thế chuỗi trong tệp
- Ví dụ 2: Thay thế chuỗi trong cùng một tệp
- Bản tóm tắt
Trong hướng dẫn này về các ví dụ Python, chúng tôi đã học cách thay thế một chuỗi bằng các tập tin khác trong tập tin, với sự trợ giúp của các ví dụ chi tiết. - Nội dung
- Giới thiệu
- Ví dụ 1: Thay thế chuỗi trong tệp
- Ví dụ 2: Thay thế chuỗi trong cùng một tệp
Ví dụ 1: Thay thế chuỗi trong tệpVí dụ 2: Thay thế chuỗi trong cùng một tệp Để thay thế một chuỗi trong tệp bằng Python, hãy làm theo các bước sau: #input file
fin = open("data.txt", "rt")
#output file to write the result to
fout = open("out.txt", "wt")
#for each line in the input file
for line in fin:
#read replace the string and write to output file
fout.write(line.replace('pyton', 'python'))
#close input and output files
fin.close()
fout.close()
Chúng ta đã làm gì ở đây? - Mở tệp đầu vào ở chế độ đọc và xử lý nó ở chế độ văn bản.
- Mở tệp đầu ra ở chế độ ghi và xử lý nó ở chế độ văn bản.
- Đối với mỗi dòng đọc từ tệp đầu vào, thay thế chuỗi và ghi vào tệp đầu ra.
- Đóng cả tệp đầu vào và đầu ra.
Tệp đầu vào Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
Tệp đầu vào tương tự sau khi thực hiện chương trình. Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
Bản tóm tắt Ví dụ 2: Thay thế chuỗi trong cùng một tệpĐể thay thế một chuỗi trong tệp bằng Python, hãy làm theo các bước sau:pyton with python in data.txt file, and
overwrite the data.txt file with the replaced text. Python prgoram #read input file
fin = open("data.txt", "rt")
#read file contents to string
data = fin.read()
#replace all occurrences of the required string
data = data.replace('pyton', 'python')
#close the input file
fin.close()
#open the input file in write mode
fin = open("data.txt", "wt")
#overrite the input file with the resulting data
fin.write(data)
#close the file
fin.close()
Chúng ta đã làm gì ở đây? - Mở tệp
do_something_public()
#chop-begin abcd
get_rid_of_me() #chop-end
#chop-beginner this should stay!
#chop-begin
do_something_private()
#chop-end The rest of this comment should go too!
but_you_need_me() #chop-begin
last_to_go()
#chop-end
5 trong Chế độ đọc văn bản do_something_public()
#chop-begin abcd
get_rid_of_me() #chop-end
#chop-beginner this should stay!
#chop-begin
do_something_private()
#chop-end The rest of this comment should go too!
but_you_need_me() #chop-begin
last_to_go()
#chop-end
8. do_something_public()
#chop-beginner this should stay!
but_you_need_me()
9 đọc toàn bộ văn bản trong do_something_public()
#chop-begin abcd
get_rid_of_me() #chop-end
#chop-beginner this should stay!
#chop-begin
do_something_private()
#chop-end The rest of this comment should go too!
but_you_need_me() #chop-begin
last_to_go()
#chop-end
5 cho biến import re
string = re.sub('#chop-begin.*?#chop-end', '', string, flags=re.DOTALL)
1.import re
string = re.sub('#chop-begin.*?#chop-end', '', string, flags=re.DOTALL)
2 thay thế tất cả các lần xuất hiện của do_something_public()
#chop-begin abcd
get_rid_of_me() #chop-end
#chop-beginner this should stay!
#chop-begin
do_something_private()
#chop-end The rest of this comment should go too!
but_you_need_me() #chop-begin
last_to_go()
#chop-end
3 bằng do_something_public()
#chop-begin abcd
get_rid_of_me() #chop-end
#chop-beginner this should stay!
#chop-begin
do_something_private()
#chop-end The rest of this comment should go too!
but_you_need_me() #chop-begin
last_to_go()
#chop-end
4 trong toàn bộ văn bản.do_something_public()
#chop-beginner this should stay!
but_you_need_me()
1 Đóng tệp đầu vào do_something_public()
#chop-begin abcd
get_rid_of_me() #chop-end
#chop-beginner this should stay!
#chop-begin
do_something_private()
#chop-end The rest of this comment should go too!
but_you_need_me() #chop-begin
last_to_go()
#chop-end
5.- Trong ba dòng cuối cùng, chúng tôi đang mở
do_something_public()
#chop-begin abcd
get_rid_of_me() #chop-end
#chop-beginner this should stay!
#chop-begin
do_something_private()
#chop-end The rest of this comment should go too!
but_you_need_me() #chop-begin
last_to_go()
#chop-end
5 trong chế độ viết văn bản import re
class Chopper(object):
def __init__(self, start='\\s*#ch'+'op-begin\\b', end='#ch'+'op-end\\b.*?$'):
super(Chopper,self).__init__()
self.re = re.compile('{0}.*?{1}'.format(start,end), flags=re.DOTALL+re.MULTILINE)
def chop(self, s):
return self.re.sub('', s)
def chopFile(self, infname, outfname=None):
if outfname is None:
outfname = infname
with open(infname) as inf:
data = inf.read()
with open(outfname, 'w') as outf:
outf.write(self.chop(data))
ch = Chopper()
ch.chopFile('data.txt')
1 và ghi dữ liệu thành do_something_public()
#chop-begin abcd
get_rid_of_me() #chop-end
#chop-beginner this should stay!
#chop-begin
do_something_private()
#chop-end The rest of this comment should go too!
but_you_need_me() #chop-begin
last_to_go()
#chop-end
5 ở chế độ thay thế. Cuối cùng đóng tệp do_something_public()
#chop-begin abcd
get_rid_of_me() #chop-end
#chop-beginner this should stay!
#chop-begin
do_something_private()
#chop-end The rest of this comment should go too!
but_you_need_me() #chop-begin
last_to_go()
#chop-end
5.
Tệp đầu vào Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
Tệp đầu vào tương tự sau khi thực hiện chương trình. Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
Bản tóm tắtTrong hướng dẫn này về các ví dụ Python, chúng tôi đã học cách thay thế một chuỗi bằng các tập tin khác trong tập tin, với sự trợ giúp của các ví dụ chi tiết. |