Python lưu mảng vào tệp và đọc

Mảng Numpy được sử dụng rộng rãi trong khi phân tích dữ liệu trong python. Trong bài viết này, chúng ta sẽ thảo luận về cách chúng ta có thể lưu một mảng có nhiều mảng vào một tệp văn bản trong python

Lưu mảng Numpy vào tệp văn bản bằng hàm str[]

Chúng ta có thể lưu một mảng có nhiều mảng vào một tệp văn bản bằng cách sử dụng hàm

The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
2 và xử lý tệp. Theo cách tiếp cận này, trước tiên chúng ta sẽ chuyển đổi mảng có nhiều mảng thành chuỗi bằng cách sử dụng hàm
The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
2. Hàm
The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
2 lấy mảng numpy làm đối số đầu vào và trả về biểu diễn chuỗi của nó. Sau khi chuyển đổi mảng numpy thành chuỗi, chúng tôi sẽ lưu chuỗi vào tệp văn bản

Để lưu mảng numpy vào một tệp văn bản, trước tiên chúng ta sẽ mở một tệp ở chế độ chắp thêm bằng cách sử dụng hàm

The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
5. Hàm
The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
5 lấy tên tệp làm đối số đầu vào đầu tiên của nó và chữ “
The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
7” làm đối số đầu vào thứ hai để biểu thị rằng tệp được mở ở chế độ chắp thêm. Sau khi thực hiện, nó trả về một đối tượng tệp chứa tệp văn bản

Sau khi lấy được đối tượng file, chúng ta sẽ sử dụng phương thức

The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
0 để lưu chuỗi chứa mảng numpy vào file. Phương thức
The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
0, khi được gọi trên một đối tượng tệp, sẽ lấy chuỗi làm đối số đầu vào của nó và nối chuỗi vào tệp. Sau khi ghi chuỗi vào tệp, đừng quên đóng tệp bằng phương thức
The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
2

Mã hoàn chỉnh để lưu một mảng có nhiều mảng vào một tệp văn bản bằng cách sử dụng hàm

The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
2 như sau

import numpy as np

myFile = open['sample.txt', 'r+']
myArray = np.array[[1, 2, 3, 4, 5, 6, 7, 8, 9]]
print["The array is:", myArray]
print["The content of the file before saving the array is:"]
text = myFile.read[]
print[text]
myFile.write[str[myArray]]
myFile.close[]
myFile = open['sample.txt', 'r']
print["The content of the file after saving the array is:"]
text = myFile.read[]
print[text]

đầu ra

The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]

Bài báo đề xuất học máy. Hồi quy trong học máy với các ví dụ

Lưu mảng Numpy vào tệp văn bản bằng cách sử dụng numpy. hàm savetxt[]

Thay vì sử dụng hàm

The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
2, chúng ta có thể sử dụng hàm
The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
5 để lưu một mảng có nhiều mảng vào một tệp văn bản trong python. Theo cách tiếp cận này, trước tiên chúng tôi mở tệp văn bản ở chế độ chắp thêm bằng cách sử dụng hàm
The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
5 như đã thảo luận trong ví dụ trước. Sau khi mở file ta sẽ sử dụng hàm the
The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
5 để lưu mảng vào file text. Ở đây, hàm
The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
5 lấy đối tượng tệp làm đối số đầu vào đầu tiên và mảng numpy làm đối số đầu vào thứ hai của nó. Sau khi thực hiện, nó lưu mảng numpy vào tệp văn bản. Bạn có thể quan sát điều này trong ví dụ sau

________số 8

đầu ra

The array is: [1 2 3 4 5 6 7 8 9]
The content of the file after saving the array is:
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
4.000000000000000000e+00
5.000000000000000000e+00
6.000000000000000000e+00
7.000000000000000000e+00
8.000000000000000000e+00
9.000000000000000000e+00

Sau khi thực hiện chức năng

The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
9, bạn phải đóng đối tượng tệp bằng cách sử dụng đối tượng
The array is: [1 2 3 4 5 6 7 8 9]
The content of the file before saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.

The content of the file after saving the array is:
I am a sample text file.
I was created by Aditya.
You are reading me at Pythonforbeginners.com.
[1 2 3 4 5 6 7 8 9]
2. Nếu không, các thay đổi sẽ không được ghi vào tệp

Phần kết luận

Trong bài viết này, chúng tôi đã thảo luận về hai cách tiếp cận để lưu một mảng có nhiều mảng vào một tệp văn bản trong python. Để biết thêm về lập trình Python, bạn có thể đọc bài viết này về hiểu danh sách trong Python. Bạn cũng có thể thích bài viết này về hiểu từ điển trong python

Có liên quan

Đào tạo Python được đề xuất

Khóa học. Python 3 cho người mới bắt đầu

Hơn 15 giờ nội dung video với hướng dẫn có hướng dẫn cho người mới bắt đầu. Tìm hiểu cách tạo các ứng dụng trong thế giới thực và nắm vững kiến ​​thức cơ bản

Làm cách nào để lưu trữ mảng trong tệp txt Python?

Sử dụng numpy. .
in [an_array]
a_file = open["kiểm tra. txt", "w"]
cho hàng trong an_array
np. savetxt[a_file, hàng]
một tập tin. close[] đóng `a_file`

Làm cách nào để đọc mảng 1D từ tệp trong Python?

nếu bạn cần số nguyên thì bạn sẽ phải chia từng dòng thành danh sách các chuỗi con và sử dụng int[] cho mọi phần tử trong danh sách này. Nếu bạn thêm một số vào danh sách thì bạn sẽ nhận được mảng 1D. Bạn cũng có thể l. extend[list] hoặc l += list để lấy mảng 1D .

Làm cách nào để chuyển đổi mảng thành CSV bằng Python?

savetext[] Phương thức này được sử dụng để lưu một mảng vào một tệp văn bản. Tạo một mảng sau đó lưu nó dưới dạng tệp CSV

Chủ Đề