Bạn cũng có thể trải qua sau:
Example:
my_list=[1,2,3,4,5,"abc","def"]
with open['your_file.txt', 'w'] as file:
for item in my_list:
file.write["%s\n" % item]
Output:
Trong
1
2
3
4
5
abc
def
4, các mục được lưu như:1
2
3
4
5
abc
def
Kịch bản của bạn cũng lưu như trên.
Nếu không, bạn có thể sử dụng dưa chua
import pickle
my_list=[1,2,3,4,5,"abc","def"]
#to write
with open['your_file.txt', 'wb'] as file:
pickle.dump[my_list, file]
#to read
with open ['your_file.txt', 'rb'] as file:
Outlist = pickle.load[file]
print[Outlist]
Đầu ra: [1, 2, 3, 4, 5, 'ABC', 'def']]
Nó lưu Dump Danh sách giống như một danh sách khi chúng tôi tải nó, chúng tôi có thể đọc.
Cũng bởi
1
2
3
4
5
abc
def
5 có thể giống như đầu ra trênimport simplejson as sj
my_list=[1,2,3,4,5,"abc","def"]
#To write
with open['your_file.txt', 'w'] as file:
sj.dump[my_list, file]
#To save
with open['your_file.txt', 'r'] as file:
mlist=sj.load[file]
print[mlist]
Giới thiệu
Các lập trình viên Python sử dụng mạnh mẽ các mảng, danh sách và từ điển như các cấu trúc dữ liệu tuần tự hóa. Lưu trữ các cấu trúc dữ liệu này liên tục yêu cầu một tệp hoặc cơ sở dữ liệu để hoạt động đúng.
Trong bài viết này, chúng ta sẽ xem cách viết danh sách vào tệp và cách đọc danh sách đó trở lại vào bộ nhớ.
Để ghi dữ liệu vào một tệp và để đọc dữ liệu từ một tệp, ngôn ngữ lập trình Python cung cấp các phương thức tiêu chuẩn
1
2
3
4
5
abc
def
6 và 1
2
3
4
5
abc
def
7 để xử lý một dòng duy nhất, cũng như 1
2
3
4
5
abc
def
8 và 1
2
3
4
5
abc
def
9 để xử lý nhiều dòng. Hơn nữa, cả mô -đun import pickle
my_list=[1,2,3,4,5,"abc","def"]
#to write
with open['your_file.txt', 'wb'] as file:
pickle.dump[my_list, file]
#to read
with open ['your_file.txt', 'rb'] as file:
Outlist = pickle.load[file]
print[Outlist]
0 và import pickle
my_list=[1,2,3,4,5,"abc","def"]
#to write
with open['your_file.txt', 'wb'] as file:
pickle.dump[my_list, file]
#to read
with open ['your_file.txt', 'rb'] as file:
Outlist = pickle.load[file]
print[Outlist]
1 cũng cho phép các cách xử lý thông minh với các bộ dữ liệu tuần tự hóa.Sử dụng các phương thức đọc [] và write []
Để đối phó với các ký tự [chuỗi] các phương pháp
1
2
3
4
5
abc
def
7 và 1
2
3
4
5
abc
def
6 cơ bản hoạt động xuất sắc. Lưu một dòng danh sách như vậy từng dòng vào tệp import pickle
my_list=[1,2,3,4,5,"abc","def"]
#to write
with open['your_file.txt', 'wb'] as file:
pickle.dump[my_list, file]
#to read
with open ['your_file.txt', 'rb'] as file:
Outlist = pickle.load[file]
print[Outlist]
4 có thể được thực hiện như sau:# Define a list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open['listfile.txt', 'w'] as filehandle:
for listitem in places:
filehandle.write[f'{listitem}\n']
import pickle
my_list=[1,2,3,4,5,"abc","def"]
#to write
with open['your_file.txt', 'wb'] as file:
pickle.dump[my_list, file]
#to read
with open ['your_file.txt', 'rb'] as file:
Outlist = pickle.load[file]
print[Outlist]
5 được mở rộng bởi một đường ngắt import pickle
my_list=[1,2,3,4,5,"abc","def"]
#to write
with open['your_file.txt', 'wb'] as file:
pickle.dump[my_list, file]
#to read
with open ['your_file.txt', 'rb'] as file:
Outlist = pickle.load[file]
print[Outlist]
6, trước tiên, sau đó được lưu trữ vào tệp đầu ra. Bây giờ chúng ta có thể xem cách đọc toàn bộ danh sách từ tệp import pickle
my_list=[1,2,3,4,5,"abc","def"]
#to write
with open['your_file.txt', 'wb'] as file:
pickle.dump[my_list, file]
#to read
with open ['your_file.txt', 'rb'] as file:
Outlist = pickle.load[file]
print[Outlist]
4 trở lại bộ nhớ:# Define an empty list
places = []
# Open the file and read the content in a list
with open['listfile.txt', 'r'] as filehandle:
for line in filehandle:
# Remove linebreak which is the last character of the string
curr_place = line[:-1]
# Add item to the list
places.append[curr_place]
Hãy nhớ rằng bạn sẽ cần phải loại bỏ đường ngắt từ cuối chuỗi. Trong trường hợp này, nó cũng giúp chúng tôi rằng Python cũng cho phép các hoạt động danh sách trên các chuỗi. Việc loại bỏ này chỉ đơn giản được thực hiện dưới dạng hoạt động danh sách trên chính chuỗi, giữ mọi thứ trừ phần tử cuối cùng. Phần tử này chứa ký tự
import pickle
my_list=[1,2,3,4,5,"abc","def"]
#to write
with open['your_file.txt', 'wb'] as file:
pickle.dump[my_list, file]
#to read
with open ['your_file.txt', 'rb'] as file:
Outlist = pickle.load[file]
print[Outlist]
6 đại diện cho độ ngắt dòng trên các hệ thống Unix/Linux.Sử dụng các phương thức WriteLines [] và Readlines []
Như đã đề cập ở đầu bài viết này, Python cũng chứa hai phương pháp -
1
2
3
4
5
abc
def
8 và 1
2
3
4
5
abc
def
9 - để viết và đọc nhiều dòng trong một bước, tương ứng. Hãy viết toàn bộ danh sách vào một tệp trên đĩa:# Define a list of places
places_list = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open['listfile.txt', 'w'] as filehandle:
filehandle.writelines[f"{place for place in places_list}\n"]
Để đọc toàn bộ danh sách từ một tệp trên đĩa chúng ta cần:
# Define an empty list
places = []
# Open the file and read the content in a list
with open['listfile.txt', 'r'] as filehandle:
filecontents = filehandle.readlines[]
for line in filecontents:
# Remove linebreak which is the last character of the string
curr_place = line[:-1]
# Add item to the list
places.append[curr_place]
Mã trên theo cách tiếp cận truyền thống hơn được mượn từ các ngôn ngữ lập trình khác. Hãy viết nó theo một cách pythonic hơn:
# Define an empty list
places = []
# Open the file and read the content in a list
with open['listfile.txt', 'r'] as filehandle:
places = [current_place.rstrip[] for current_place in filehandle.readlines[]]
Thứ nhất, nội dung tệp được đọc qua
1
2
3
4
5
abc
def
9. Thứ hai, trong vòng lặp import simplejson as sj
my_list=[1,2,3,4,5,"abc","def"]
#To write
with open['your_file.txt', 'w'] as file:
sj.dump[my_list, file]
#To save
with open['your_file.txt', 'r'] as file:
mlist=sj.load[file]
print[mlist]
2 từ mỗi dòng, ký tự ngắt dòng được loại bỏ bằng phương pháp import simplejson as sj
my_list=[1,2,3,4,5,"abc","def"]
#To write
with open['your_file.txt', 'w'] as file:
sj.dump[my_list, file]
#To save
with open['your_file.txt', 'r'] as file:
mlist=sj.load[file]
print[mlist]
3. Thứ ba, chuỗi được thêm vào danh sách các địa điểm dưới dạng mục danh sách mới.So với danh sách trước khi mã nhỏ gọn hơn nhiều, nhưng có thể khó đọc hơn đối với các lập trình viên Python mới bắt đầu.
Sử dụng mô -đun joblib
Các phương pháp ban đầu được giải thích cho đến bây giờ lưu trữ danh sách theo cách mà con người vẫn có thể đọc nó - hoàn toàn là một danh sách tuần tự trong một tệp. Điều này là tuyệt vời để tạo các báo cáo đơn giản hoặc xuất các tệp xuất cho người dùng để sử dụng thêm, chẳng hạn như các tệp CSV. Tuy nhiên - nếu mục đích của bạn là chỉ tuần tự hóa danh sách vào một tệp, điều đó có thể được tải sau đó, không cần phải lưu trữ nó ở định dạng có thể đọc được của con người.
Mô -đun
import simplejson as sj
my_list=[1,2,3,4,5,"abc","def"]
#To write
with open['your_file.txt', 'w'] as file:
sj.dump[my_list, file]
#To save
with open['your_file.txt', 'r'] as file:
mlist=sj.load[file]
print[mlist]
4 cung cấp cách dễ nhất để đổ đối tượng Python [có thể là bất kỳ đối tượng nào thực sự]:import joblib
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
# Dumps into file
joblib.dump[places, 'places.sav']
# Loads from file
places = joblib.load['places.sav']
print[places] # ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
import simplejson as sj
my_list=[1,2,3,4,5,"abc","def"]
#To write
with open['your_file.txt', 'w'] as file:
sj.dump[my_list, file]
#To save
with open['your_file.txt', 'r'] as file:
mlist=sj.load[file]
print[mlist]
4 vẫn là cách đơn giản nhất và sạch nhất để tuần tự hóa các đối tượng theo định dạng hiệu quả và tải chúng sau. Bạn có thể sử dụng bất kỳ định dạng tùy ý nào, chẳng hạn như import simplejson as sj
my_list=[1,2,3,4,5,"abc","def"]
#To write
with open['your_file.txt', 'w'] as file:
sj.dump[my_list, file]
#To save
with open['your_file.txt', 'r'] as file:
mlist=sj.load[file]
print[mlist]
6, import simplejson as sj
my_list=[1,2,3,4,5,"abc","def"]
#To write
with open['your_file.txt', 'w'] as file:
sj.dump[my_list, file]
#To save
with open['your_file.txt', 'r'] as file:
mlist=sj.load[file]
print[mlist]
7, v.v ... Nó không thực sự quan trọng - cả import simplejson as sj
my_list=[1,2,3,4,5,"abc","def"]
#To write
with open['your_file.txt', 'w'] as file:
sj.dump[my_list, file]
#To save
with open['your_file.txt', 'r'] as file:
mlist=sj.load[file]
print[mlist]
4 và các lựa chọn thay thế như import pickle
my_list=[1,2,3,4,5,"abc","def"]
#to write
with open['your_file.txt', 'wb'] as file:
pickle.dump[my_list, file]
#to read
with open ['your_file.txt', 'rb'] as file:
Outlist = pickle.load[file]
print[Outlist]
0 sẽ đọc các tệp tốt.Sử dụng mô -đun Pickle
Thay thế cho
import simplejson as sj
my_list=[1,2,3,4,5,"abc","def"]
#To write
with open['your_file.txt', 'w'] as file:
sj.dump[my_list, file]
#To save
with open['your_file.txt', 'r'] as file:
mlist=sj.load[file]
print[mlist]
4, chúng ta có thể sử dụng import pickle
my_list=[1,2,3,4,5,"abc","def"]
#to write
with open['your_file.txt', 'wb'] as file:
pickle.dump[my_list, file]
#to read
with open ['your_file.txt', 'rb'] as file:
Outlist = pickle.load[file]
print[Outlist]
0! Phương thức # Define a list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open['listfile.txt', 'w'] as filehandle:
for listitem in places:
filehandle.write[f'{listitem}\n']
2 của nó lưu trữ danh sách hiệu quả dưới dạng luồng dữ liệu nhị phân. Thứ nhất, tệp đầu ra # Define a list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open['listfile.txt', 'w'] as filehandle:
for listitem in places:
filehandle.write[f'{listitem}\n']
3 được mở để viết nhị phân [# Define a list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open['listfile.txt', 'w'] as filehandle:
for listitem in places:
filehandle.write[f'{listitem}\n']
4]. Thứ hai, danh sách được lưu trữ trong tệp đã mở bằng phương thức # Define a list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open['listfile.txt', 'w'] as filehandle:
for listitem in places:
filehandle.write[f'{listitem}\n']
2:1
2
3
4
5
abc
def
0Là bước tiếp theo, chúng tôi đọc danh sách từ tệp như sau. Thứ nhất, tệp đầu ra
# Define a list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open['listfile.txt', 'w'] as filehandle:
for listitem in places:
filehandle.write[f'{listitem}\n']
3 được mở nhị phân để đọc [# Define a list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open['listfile.txt', 'w'] as filehandle:
for listitem in places:
filehandle.write[f'{listitem}\n']
7]. Thứ hai, danh sách các địa điểm được tải từ tệp bằng phương thức # Define a list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open['listfile.txt', 'w'] as filehandle:
for listitem in places:
filehandle.write[f'{listitem}\n']
8:1
2
3
4
5
abc
def
1Hai ví dụ ở đây chứng minh việc sử dụng chuỗi. Mặc dù,
import pickle
my_list=[1,2,3,4,5,"abc","def"]
#to write
with open['your_file.txt', 'wb'] as file:
pickle.dump[my_list, file]
#to read
with open ['your_file.txt', 'rb'] as file:
Outlist = pickle.load[file]
print[Outlist]
0 hoạt động với tất cả các loại đối tượng Python như chuỗi, số, cấu trúc tự xác định và mọi cấu trúc dữ liệu tích hợp khác cung cấp.Sử dụng định dạng JSON
Định dạng dữ liệu nhị phân
import pickle
my_list=[1,2,3,4,5,"abc","def"]
#to write
with open['your_file.txt', 'wb'] as file:
pickle.dump[my_list, file]
#to read
with open ['your_file.txt', 'rb'] as file:
Outlist = pickle.load[file]
print[Outlist]
0 sử dụng dành riêng cho Python. Để cải thiện khả năng tương tác giữa các chương trình khác nhau, ký hiệu đối tượng JavaScript [JSON] cung cấp một lược đồ dễ sử dụng và có thể đọc được, và do đó trở nên rất phổ biến để tuần tự hóa các tệp và chia sẻ chúng qua API.Kiểm tra hướng dẫn thực hành của chúng tôi, thực tế để học Git, với các thực hành tốt nhất, các tiêu chuẩn được công nghiệp chấp nhận và bao gồm bảng gian lận. Ngừng các lệnh git googling và thực sự tìm hiểu nó!
Ví dụ sau đây cho thấy cách viết danh sách các loại biến hỗn hợp vào tệp đầu ra bằng mô -đun JSON. Sau khi mở tệp đầu ra để viết, phương thức
# Define a list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open['listfile.txt', 'w'] as filehandle:
for listitem in places:
filehandle.write[f'{listitem}\n']
2 lưu trữ danh sách cơ bản trong tệp bằng ký hiệu JSON:1
2
3
4
5
abc
def
2Đọc nội dung của tệp đầu ra trở lại vào bộ nhớ cũng đơn giản như viết dữ liệu. Phương pháp tương ứng với
# Define a list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open['listfile.txt', 'w'] as filehandle:
for listitem in places:
filehandle.write[f'{listitem}\n']
2 được đặt tên # Define a list of places
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open['listfile.txt', 'w'] as filehandle:
for listitem in places:
filehandle.write[f'{listitem}\n']
8:1
2
3
4
5
abc
def
3Sự kết luận
Các phương thức khác nhau, chúng tôi đã hiển thị ở trên phạm vi từ dữ liệu viết/đọc đơn giản cho đến việc đổ/tải dữ liệu qua các luồng nhị phân bằng Pickle và JSON. Điều này đơn giản hóa việc lưu trữ một danh sách một cách kiên trì và đọc lại vào bộ nhớ.