Hướng dẫn how to number lines in python - cách đánh số dòng trong python

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!"]
2
1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
3
1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
4
1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
5
1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
6
1   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!"]
8

1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
9
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]
0
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
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]
2
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]
3

1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
9
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]
5
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]
6
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]
7
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]
8

Output:

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!"]
2
1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
3
1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
4
1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
5
1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
6
1   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!"]
8

1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
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]]
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]]
8
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]]
9
newFile = open[yourfile, 'w']
count = 1

for line in readfile:
    newFile.write [str[count] + '\t' + line]
    count += 1
newFile.close[]
0
newFile = open[yourfile, 'w']
count = 1

for line in readfile:
    newFile.write [str[count] + '\t' + line]
    count += 1
newFile.close[]
1

newFile = open[yourfile, 'w']
count = 1

for line in readfile:
    newFile.write [str[count] + '\t' + line]
    count += 1
newFile.close[]
2
newFile = open[yourfile, 'w']
count = 1

for line in readfile:
    newFile.write [str[count] + '\t' + line]
    count += 1
newFile.close[]
3

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]
5
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]
6
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]
7
newFile = open[yourfile, 'w']
count = 1

for line in readfile:
    newFile.write [str[count] + '\t' + line]
    count += 1
newFile.close[]
7
newFile = 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[]
9
for lines in openfile:
    print str[count] + '\t' + lines
    count += 1
0

Output:

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!"]
3
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]
6
for lines in openfile:
    print str[count] + '\t' + lines
    count += 1
5
1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
6
for lines in openfile:
    print str[count] + '\t' + lines
    count += 1
7
for lines in openfile:
    print str[count] + '\t' + lines
    count += 1
0

for lines in openfile:
    print str[count] + '\t' + lines
    count += 1
9
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
with open[path] as src:
    for index, line in enumerate[src.readlines[], start=1]:
        print '{:4d}: {}'.format[index, line.rstrip[]]
1

with open[path] as src:
    for index, line in enumerate[src.readlines[], start=1]:
        print '{:4d}: {}'.format[index, line.rstrip[]]
2
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
for lines in openfile:
    print str[count] + '\t' + lines
    count += 1
1
with open[path] as src:
    for index, line in enumerate[src.readlines[], start=1]:
        print '{:4d}: {}'.format[index, line.rstrip[]]
5

with open[path] as src:
    for index, line in enumerate[src.readlines[], start=1]:
        print '{:4d}: {}'.format[index, line.rstrip[]]
6
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
with open[path] as src:
    for index, line in enumerate[src.readlines[], start=1]:
        print '{:4d}: {}'.format[index, line.rstrip[]]
8
with open[path] as src:
    for index, line in enumerate[src.readlines[], start=1]:
        print '{:4d}: {}'.format[index, line.rstrip[]]
9
for lines in openfile:
    print str[count] + '\t' + lines
    count += 1
0

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]]
7
with open[path] as src:
    print '\n'.join[['{:4d}: {}'.format[i, x.rstrip[]] for i, x in enumerate[src.readlines[], start=1]]]
2
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]]
9
with open[path] as src:
    print '\n'.join[['{:4d}: {}'.format[i, x.rstrip[]] for i, x in enumerate[src.readlines[], start=1]]]
4

1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
9
with 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]]]
7

newFile = open[yourfile, 'w']
count = 1

for line in readfile:
    newFile.write [str[count] + '\t' + line]
    count += 1
newFile.close[]
2
for lines in openfile:
    print str[count] + '\t' + lines
    count += 1
9
newFile = open[yourfile, 'w']
count = 1

for line in readfile:
    newFile.write [str[count] + '\t' + line]
    count += 1
newFile.close[]
8
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
newFile = open[yourfile, 'w']
count = 1

for line in readfile:
    newFile.write [str[count] + '\t' + line]
    count += 1
newFile.close[]
9

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]
5
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]
6
Total Number of lines: 5
5
for lines in openfile:
    print str[count] + '\t' + lines
    count += 1
0

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]
5
Total Number of lines: 5
8

Output:

1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
0

Phươ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!"]
2
1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
3
1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
4
1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
5
1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
6
1   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!"]
9
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]
0__21
Total Number of lines: 5
9
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]
6
newFile = 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]]
7

1   def main[]:
2       for i in range[10]:
3           print["I love Python"]
4       print["Good bye!"]
9
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]
5
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]
6
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]
7
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]
8

Output:

Total Number of lines: 5

Làm thế nào để bạn viết các dòng trong Python?

Để ghi vào tệp văn bản bằng Python, bạn làm theo các bước sau: Đầu tiên, hãy mở tệp văn bản để ghi [hoặc nối] bằng hàm Open []..Thứ ba, đóng tệp bằng phương thức đóng [].open the text file for writing [or append] using the open[] function. Second, write to the text file using the write[] or writelines[] method. Third, close the file using the close[] method.

Làm thế nào để bạn đi đến dòng 2 trong Python?

Bạn không thể chia một câu lệnh thành nhiều dòng trong Python bằng cách nhấn Enter.Thay vào đó, hãy sử dụng dấu gạch chéo ngược [\] để chỉ ra rằng một câu lệnh được tiếp tục trên dòng tiếp theo.use the backslash [ \ ] to indicate that a statement is continued on the next line.

Làm cách nào để in số trên một dòng khác trong Python?

Sử dụng một vòng lặp để lặp qua chuỗi.Sử dụng hàm in [] để in từng số trên một dòng riêng biệt.Use the print[] function to print each number on a separate line.

Bài Viết Liên Quan

Chủ Đề