30/09/2018, 23:48

Mọi người xem bài này của em vì sao ko in ra dc số 1, 2 ở phần chạy lại, chữ bị tách ra. Có phải là lỗi của Notepad++ hay Powershell ko?

from sys import argv

script, input_file = argv

def print_all(f):
	print f.read()
	
def rewind(f):
	f.seek(0)
	
def print_a_line(line_count, f):
	print line_count, f.readline()
	
current_file = open(input_file)

print "First let's print the whole file:
"

print_all(current_file)

print "Now let's rewind, kind of like a tape."

rewind(current_file)

print "Let's print three lines:"

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1 
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

và 1 file txt

This is line 1
This is line 2
This is line 3

Khoa NTA viết 02:05 ngày 01/10/2018

Bởi vì readline() sẽ trả về toàn bộ dòng đó, bao gồm cả '\n'. Mà lệnh print thì đã tự xuống dòng nên bạn chỉ cần chỉnh sửa 1 chút ở hàm print_a_line:

  • như thế này: print line_count, f.readline().strip('\n')
  • hoặc đơn giản hơn: print line_count, f.readline()[:-1]

Còn đoạn 1 bình thường là tại vì read cũng đọc hết file, file có bao nhiêu '\n' thì nó hiện bấy nhiêu thôi.

Ikaros viết 01:56 ngày 01/10/2018

sao ko dc vậy bạn. Nó in như thế này:
First let’s print the whole file:

■T h i s i s l i n e 1
T h i s i s l i n e 2
T h i s i s l i n e 3
Now let’s rewind, kind of like a tape
Let’s print three lines:
■T h i s i s l i n e 1

T h i s i s l i n e 2

3 T h i s i s l i n e 3
còn cái [:-1] thì nó báo lỗi TypeError: ‘builtin_function_or_method’ object has no attribute ‘getitem

Khoa NTA viết 01:52 ngày 01/10/2018

Kỳ vậy, mình test thành công rồi mới up lên mà. Mình vừa test lại thì không có lỗi, bạn kiểm tra lại code xem. Nếu bạn đang ở Windows thì bỏ 2 ký tự ‘\n’ và ‘\r’ nha.

#!/usr/bin/env python
from sys import argv

script, input_file = argv

def print_all(f):
    print f.read()
    
def rewind(f):
    f.seek(0)
    
def print_a_line(line_count, f):
    print line_count, f.readline()[:-1]
    
current_file = open(input_file)

print "First let's print the whole file:\n"

print_all(current_file)

print "Now let's rewind, kind of like a tape."

rewind(current_file)

print "Let's print three lines:"

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1 
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

Output:

First let’s print the whole file:

This is line 1
This is line 2
This is line 3

Now let’s rewind, kind of like a tape.
Let’s print three lines:
1 This is line 1
2 This is line 2
3 This is line 3

Phan Hoàng viết 01:58 ngày 01/10/2018

Mấy cái hình vuông kia là do Notepad của Microsoft nó cho thêm 3 ký tự dùng làm chữ ký, gọi là BOM (byte order mark). Bạn mở Notepad, chọn save as UTF8 without BOM hoặc ANSI.

(hoặc tốt nhất, kể cả file text cũng nên dùng Notepad++ mà edit, đừng dùng Notepad của MS)

Lương Quang Mạnh viết 02:02 ngày 01/10/2018

Code mình cũng chạy ngon lành cành đào mà, sao lỗi nhỉ?
Rất có thể là bạn thiết 2 cái ngoặc tròn trong f.readline()[:-1] rồi.

Ikaros viết 02:05 ngày 01/10/2018

cảm ơn các bạn, mình đã in đc rồi. Sau này mình sẽ còn nhiều thắc mắc, mong các bạn giúp đỡ nha

Bài liên quan
0