# Code in dữ liệu từ file bị tách chữ ra

**URL:** https://daynhauhoc.com/t/code-in-du-lieu-tu-file-bi-tach-chu-ra/34706
**Category:** programming
**Tags:** python
**Created:** [September 14, 2016, 12:39am UTC](https://daynhauhoc.com/t/code-in-du-lieu-tu-file-bi-tach-chu-ra/34706 "2016-09-14T00:39:55Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![t2001kiet](https://daynhauhoc.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@t2001kiet](https://daynhauhoc.com/u/t2001kiet)
#### Post date: [September 14, 2016, 12:39am UTC](https://daynhauhoc.com/t/code-in-du-lieu-tu-file-bi-tach-chu-ra/34706/1 "2016-09-14T00:39:55Z")

</div>

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?

```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()
	
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)

```

**và 1 file txt**

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

---

<div class="post-metadata">

### Author: ![nicklauri](https://daynhauhoc.com/user_avatar/daynhauhoc.com/nicklauri/32/29433_2.png) [@nicklauri](https://daynhauhoc.com/u/nicklauri)
#### Post date: [September 14, 2016, 1:05am UTC](https://daynhauhoc.com/t/code-in-du-lieu-tu-file-bi-tach-chu-ra/34706/2 "2016-09-14T01:05:18Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![t2001kiet](https://daynhauhoc.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@t2001kiet](https://daynhauhoc.com/u/t2001kiet)
#### Post date: [September 14, 2016, 2:44am UTC](https://daynhauhoc.com/t/code-in-du-lieu-tu-file-bi-tach-chu-ra/34706/3 "2016-09-14T02:44:14Z")

</div>

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** ’

---

<div class="post-metadata">

### Author: ![nicklauri](https://daynhauhoc.com/user_avatar/daynhauhoc.com/nicklauri/32/29433_2.png) [@nicklauri](https://daynhauhoc.com/u/nicklauri)
#### Post date: [September 14, 2016, 4:05am UTC](https://daynhauhoc.com/t/code-in-du-lieu-tu-file-bi-tach-chu-ra/34706/4 "2016-09-14T04:05:54Z")

</div>

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.

```auto
#!/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

---

<div class="post-metadata">

### Author: ![Phan\_Hoang](https://daynhauhoc.com/user_avatar/daynhauhoc.com/phan_hoang/32/37488_2.png) [@Phan\_Hoang](https://daynhauhoc.com/u/Phan_Hoang)
#### Post date: [September 14, 2016, 9:34am UTC](https://daynhauhoc.com/t/code-in-du-lieu-tu-file-bi-tach-chu-ra/34706/5 "2016-09-14T09:34:10Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![Luong\_Quang\_Manh](https://daynhauhoc.com/user_avatar/daynhauhoc.com/luong_quang_manh/32/15342_2.png) [@Luong\_Quang\_Manh](https://daynhauhoc.com/u/Luong_Quang_Manh)
#### Post date: [September 14, 2016, 10:12am UTC](https://daynhauhoc.com/t/code-in-du-lieu-tu-file-bi-tach-chu-ra/34706/6 "2016-09-14T10:12:36Z")

</div>

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`.

---

<div class="post-metadata">

### Author: ![t2001kiet](https://daynhauhoc.com/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@t2001kiet](https://daynhauhoc.com/u/t2001kiet)
#### Post date: [September 14, 2016, 2:16pm UTC](https://daynhauhoc.com/t/code-in-du-lieu-tu-file-bi-tach-chu-ra/34706/7 "2016-09-14T14:16:29Z")

</div>

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
