Ví dụ: nếu tệp đầu vào là:
def main[]:
for i in range[10]:
print["I love Python"]
print["Good bye!"]
Sau đó, đầu ra sẽ là:
1 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
Tôi gặp khó khăn trong việc thêm các dòng vào mỗi dòng. Chương trình của tôi là:
filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
Đã hỏi ngày 25 tháng 3 năm 2015 lúc 23:09Mar 25, 2015 at 23:09
3
Sử dụng một câu lệnh để đóng bộ đệm tệp và chỉ là chuỗi Concatenate:
with open['file.txt', 'r'] as program:
data = program.readlines[]
with open['file.txt', 'w'] as program:
for [number, line] in enumerate[data]:
program.write['%d %s' % [number + 1, line]]
Đã trả lời ngày 25 tháng 3 năm 2015 lúc 23:14Mar 25, 2015 at 23:14
Malik Brahimimalik BrahimiMalik Brahimi
16.1k5 Huy hiệu vàng36 Huy hiệu bạc66 Huy hiệu Đồng5 gold badges36 silver badges66 bronze badges
2
Bạn nên thêm:
newFile = open[yourfile, 'w']
count = 1
for line in readfile:
newFile.write [str[count] + '\t' + line]
count += 1
newFile.close[]
Nếu bạn chỉ muốn in để ghi bàn điều khiển [đây là theo các tên biến bạn đã sử dụng trong chỉnh sửa thứ hai của mình]:
for lines in openfile:
print str[count] + '\t' + lines
count += 1
Bạn nên làm bài tập về nhà của bạn, mặc dù!
Đã trả lời ngày 25 tháng 3 năm 2015 lúc 23:16Mar 25, 2015 at 23:16
user823743user823743user823743
2.1243 huy hiệu vàng21 Huy hiệu bạc31 Huy hiệu đồng3 gold badges21 silver badges31 bronze badges
10
Tôi sẽ viết như vậy:
with open[path] as src:
for index, line in enumerate[src.readlines[], start=1]:
print '{:4d}: {}'.format[index, line.rstrip[]]
hoặc
with open[path] as src:
print '\n'.join[['{:4d}: {}'.format[i, x.rstrip[]] for i, x in enumerate[src.readlines[], start=1]]]
Đã trả lời ngày 20 tháng 7 năm 2017 lúc 17:10Jul 20, 2017 at 17:10
Điều kiện tiên quyết: Xử lý tập tin trong Python & NBSP; File Handling in Python
Đếm số lượng ký tự là quan trọng vì hầu hết các hộp văn bản dựa vào đầu vào của người dùng đều có giới hạn nhất định về số lượng ký tự có thể được chèn. Ví dụ: nếu tệp nhỏ, bạn có thể sử dụng readlines [] hoặc cách tiếp cận vòng lặp. Nếu bạn có nhiều tệp để làm việc để xác định số lượng dòng, hãy sử dụng giao diện máy phát và giao diện thô, & nbsp; nếu bạn không muốn & nbsp; tải tệp hoàn chỉnh vào, hãy sử dụng vòng lặp và liệt kê [] cho các tệp khổng lồ. Chương trình này trong xử lý tệp dữ liệu trong Python nhấn mạnh việc đếm số lượng dòng trong một tệp văn bản trong Python. & NBSP;
Dưới đây là việc thực hiện. & NBSP;
Hãy giả sử tệp văn bản trông như thế này - & nbsp;
myfile.txt
Phương pháp 1: Số lượng Python số dòng trong chuỗi bằng cách sử dụng đường đọc []
Các đường đọc [] được sử dụng để đọc tất cả các dòng trong một lần và sau đó trả về chúng dưới dạng mỗi dòng một phần tử chuỗi trong một danh sách. Chức năng này có thể được sử dụng cho các tệp nhỏ
Python3
1 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
21 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
31 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
41 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
51 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
61 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
71 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
81 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
9filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
0filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
1 filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
2filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
31 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
9filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
5filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
6filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
7filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
8Output:
Total Number of lines: 5
Phương pháp 2: Số lượng dòng Python trong tệp văn bản bằng cách sử dụng liệt kê
Phương thức liệt kê [] thêm một bộ đếm cho một điều đó và trả lại nó trong một hình thức liệt kê đối tượng. & NBSP;
Python3
1 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
21 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
31 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
41 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
51 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
61 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
71 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
81 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
9with open['file.txt', 'r'] as program:
data = program.readlines[]
with open['file.txt', 'w'] as program:
for [number, line] in enumerate[data]:
program.write['%d %s' % [number + 1, line]]
7 with open['file.txt', 'r'] as program:
data = program.readlines[]
with open['file.txt', 'w'] as program:
for [number, line] in enumerate[data]:
program.write['%d %s' % [number + 1, line]]
8with open['file.txt', 'r'] as program:
data = program.readlines[]
with open['file.txt', 'w'] as program:
for [number, line] in enumerate[data]:
program.write['%d %s' % [number + 1, line]]
9 newFile = open[yourfile, 'w']
count = 1
for line in readfile:
newFile.write [str[count] + '\t' + line]
count += 1
newFile.close[]
0newFile = open[yourfile, 'w']
count = 1
for line in readfile:
newFile.write [str[count] + '\t' + line]
count += 1
newFile.close[]
1newFile = open[yourfile, 'w']
count = 1
for line in readfile:
newFile.write [str[count] + '\t' + line]
count += 1
newFile.close[]
2newFile = open[yourfile, 'w']
count = 1
for line in readfile:
newFile.write [str[count] + '\t' + line]
count += 1
newFile.close[]
3filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
5filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
6filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
7newFile = open[yourfile, 'w']
count = 1
for line in readfile:
newFile.write [str[count] + '\t' + line]
count += 1
newFile.close[]
7newFile = open[yourfile, 'w']
count = 1
for line in readfile:
newFile.write [str[count] + '\t' + line]
count += 1
newFile.close[]
8 newFile = open[yourfile, 'w']
count = 1
for line in readfile:
newFile.write [str[count] + '\t' + line]
count += 1
newFile.close[]
9for lines in openfile:
print str[count] + '\t' + lines
count += 1
0Output:
Total Number of lines: 5
Phương pháp 3: Sử dụng vòng lặp và bộ đếm cho các dòng đếm
Vòng lặp Python được sử dụng cho các lần đi ngang tuần tự. Ở đây chúng tôi sẽ đếm số lượng dòng bằng cách sử dụng vòng lặp và câu lệnh IF. Nếu giúp chúng tôi xác định có bất kỳ ký tự nào hay không, nếu sẽ có bất kỳ ký tự nào có điều kiện nếu điều kiện trả về đúng và bộ đếm sẽ tăng lên.
Python3
for lines in openfile:
print str[count] + '\t' + lines
count += 1
1 filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
1 1 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
3filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
6for lines in openfile:
print str[count] + '\t' + lines
count += 1
51 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
6for lines in openfile:
print str[count] + '\t' + lines
count += 1
7for lines in openfile:
print str[count] + '\t' + lines
count += 1
0for lines in openfile:
print str[count] + '\t' + lines
count += 1
9filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
1 with open[path] as src:
for index, line in enumerate[src.readlines[], start=1]:
print '{:4d}: {}'.format[index, line.rstrip[]]
1with open[path] as src:
for index, line in enumerate[src.readlines[], start=1]:
print '{:4d}: {}'.format[index, line.rstrip[]]
2filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
1 for lines in openfile:
print str[count] + '\t' + lines
count += 1
1with open[path] as src:
for index, line in enumerate[src.readlines[], start=1]:
print '{:4d}: {}'.format[index, line.rstrip[]]
5with open[path] as src:
for index, line in enumerate[src.readlines[], start=1]:
print '{:4d}: {}'.format[index, line.rstrip[]]
6filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
1 with open[path] as src:
for index, line in enumerate[src.readlines[], start=1]:
print '{:4d}: {}'.format[index, line.rstrip[]]
8with open[path] as src:
for index, line in enumerate[src.readlines[], start=1]:
print '{:4d}: {}'.format[index, line.rstrip[]]
9for lines in openfile:
print str[count] + '\t' + lines
count += 1
0with open['file.txt', 'r'] as program:
data = program.readlines[]
with open['file.txt', 'w'] as program:
for [number, line] in enumerate[data]:
program.write['%d %s' % [number + 1, line]]
7 with open[path] as src:
print '\n'.join[['{:4d}: {}'.format[i, x.rstrip[]] for i, x in enumerate[src.readlines[], start=1]]]
2with open['file.txt', 'r'] as program:
data = program.readlines[]
with open['file.txt', 'w'] as program:
for [number, line] in enumerate[data]:
program.write['%d %s' % [number + 1, line]]
9 with open[path] as src:
print '\n'.join[['{:4d}: {}'.format[i, x.rstrip[]] for i, x in enumerate[src.readlines[], start=1]]]
41 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
9with open[path] as src:
print '\n'.join[['{:4d}: {}'.format[i, x.rstrip[]] for i, x in enumerate[src.readlines[], start=1]]]
6 with open[path] as src:
print '\n'.join[['{:4d}: {}'.format[i, x.rstrip[]] for i, x in enumerate[src.readlines[], start=1]]]
7newFile = open[yourfile, 'w']
count = 1
for line in readfile:
newFile.write [str[count] + '\t' + line]
count += 1
newFile.close[]
2for lines in openfile:
print str[count] + '\t' + lines
count += 1
9newFile = open[yourfile, 'w']
count = 1
for line in readfile:
newFile.write [str[count] + '\t' + line]
count += 1
newFile.close[]
8filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
1 newFile = open[yourfile, 'w']
count = 1
for line in readfile:
newFile.write [str[count] + '\t' + line]
count += 1
newFile.close[]
9filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
5filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
6Total Number of lines: 55
for lines in openfile:
print str[count] + '\t' + lines
count += 1
0filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
5Total Number of lines: 58
Output:
1 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
0Phương pháp 4: Sử dụng chức năng Loop và Sum để đếm các dòng
Vòng lặp Python được sử dụng cho các lần đi ngang tuần tự. Ở đây chúng tôi sẽ đếm số lượng dòng sử dụng hàm tổng trong Python. Nếu giúp chúng tôi xác định có bất kỳ ký tự nào hay không, nếu sẽ có bất kỳ ký tự nào có điều kiện nếu điều kiện trả về đúng và bộ đếm sẽ tăng lên.
Python3
1 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
21 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
31 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
41 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
51 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
61 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
7__1 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
9filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
0__21 Total Number of lines: 59
filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
6newFile = open[yourfile, 'w']
count = 1
for line in readfile:
newFile.write [str[count] + '\t' + line]
count += 1
newFile.close[]
9 with open['file.txt', 'r'] as program:
data = program.readlines[]
with open['file.txt', 'w'] as program:
for [number, line] in enumerate[data]:
program.write['%d %s' % [number + 1, line]]
71 def main[]:
2 for i in range[10]:
3 print["I love Python"]
4 print["Good bye!"]
9filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
5filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
6filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
7filename = input["Please enter a file name: "]
count = 0
openfile = open[filename, "r"]
for lines in openfile:
linenumbers = openfile.write[str[count]+'\t'+lines]
count += 1
print[count]
8Output:
Total Number of lines: 5