Tôi đã thu thập dữ liệu dưới dạng danh sách danh sách và viết dữ liệu vào một tệp văn bản. Dữ liệu trong tệp văn bản trông giống như
[[123231,2345,888754],[223467,85645]]
Tôi muốn đọc lại và lưu trữ trong danh sách các danh sách trong chương trình của tôi. Nhưng khi tôi thực hiện
import json
with open[myfilename] as f:
lst = json.load[f]
0 từ tệp và thử tạo một danh sách phẳng thì nó sẽ lấy mọi thứ như một chuỗi và việc giải thích thay đổi hoàn toàn và tôi không thể truy vấn kết quả tôi nhận được sau khi đọc dưới dạng danh sách bình thường trong danh sách trong Python.Ai đó có thể giúp tôi đọc tệp và lưu trữ ở cùng định dạng như danh sách danh sách không?
Cảm ơn bạn!
HOCHL
12k10 Huy hiệu vàng54 Huy hiệu bạc84 Huy hiệu đồng10 gold badges54 silver badges84 bronze badges
Hỏi ngày 24 tháng 4 năm 2012 lúc 8:26Apr 24, 2012 at 8:26
Richa Sachdevricha SachdevRicha Sachdev
2.8273 huy hiệu vàng16 Huy hiệu bạc12 Huy hiệu đồng3 gold badges16 silver badges12 bronze badges
1
Điều này trông giống như JSON hợp lệ.
Vì vậy, bạn có thể chỉ cần làm:
import json
with open[myfilename] as f:
lst = json.load[f]
Để lưu trữ "danh sách danh sách" của bạn trong một tệp, hãy làm
with open[myfilename, "w"] as f:
json.dump[lst, f]
Đã trả lời ngày 24 tháng 4 năm 2012 lúc 8:30Apr 24, 2012 at 8:30
Tim Pietzckertim PietzckerTim Pietzcker
Phù bằng vàng 319K5757 gold badges494 silver badges550 bronze badges
3
Thay vào đó, bạn chỉ có thể dồn dữ liệu của mình. Để lưu nó:
>>> import pickle
>>> p= [[123231,2345,888754],[223467,85645]]
>>> with open["data.txt", "wb"] as internal_filename:
... pickle.dump[p, internal_filename]
Để tải nó:
>>> with open["data.txt", "rb"] as new_filename:
... pp = pickle.load[new_filename]
>>> pp
[[123231, 2345, 888754], [223467, 85645]]
Điều này cũng hữu ích cho các đối tượng phức tạp hơn nhiều.
Đã trả lời ngày 24 tháng 4 năm 2012 lúc 8:34Apr 24, 2012 at 8:34
Ngoài ra, bạn có thể sử dụng
import json
with open[myfilename] as f:
lst = json.load[f]
1 để đạt được điều này, nhưng các giải pháp khác có thể tốt hơn:# reading a list from a file
f = open['data.txt', 'r']
l = eval[f.read[]]
# storing a list to a file
outf = open['out','w']
outf.write[str[l]]
Thỉnh thoảng sử dụng
import json
with open[myfilename] as f:
lst = json.load[f]
1 là một thực tế xấu. Để biết thêm thông tin, hãy kiểm tra bài đăng này.
Đã trả lời ngày 24 tháng 4 năm 2012 lúc 8:43Apr 24, 2012 at 8:43
Than Petsasthanocation PetsasThanasis Petsas
4.2785 Huy hiệu vàng30 Huy hiệu bạc56 Huy hiệu Đồng5 gold badges30 silver badges56 bronze badges
0
Đảm bảo rằng:
- Tệp mà dữ liệu danh sách đang ở là một tệp python
- dữ liệu danh sách được gán cho một biến
- Tệp nằm trong mô -đun Python
Sau đó, bạn có thể nhập danh sách từ tệp Python. Ví dụ: nói rằng danh sách đó, my_list, có trong tệp mylist.py
E.g:
# This is the file mylist.py
my_list = [[123231,2345,888754],[223467,85645]]
Sau đó, bạn có thể coi my_list là một danh sách python
from mylist import my_list
for item in mylist:
print "item = "
Đã trả lời ngày 24 tháng 4 năm 2012 lúc 8:56Apr 24, 2012 at 8:56
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Trong bài viết này, chúng ta sẽ xem cách đọc các tệp văn bản vào danh sách trong Python.
Tệp để trình diễn:
Ví dụ 1: Chuyển đổi tệp văn bản thành một danh sách bằng cách chia văn bản về sự xuất hiện của ‘.The read bytes are returned as a string. Reads n bytes, or the full file if no n is given.
Chúng tôi mở tệp trong chế độ đọc, sau đó đọc tất cả văn bản bằng cách đọc [] và lưu trữ nó vào một biến có tên là dữ liệu. Sau đó, chúng tôi thay thế phần cuối của dòng [‘/n,] bằng‘ ‘và chia văn bản hơn nữa khi nhìn thấy bằng cách sử dụng các hàm chia [] và thay thế []. & nbsp;
Đọc []: Các byte đọc được trả về dưới dạng chuỗi. Đọc n byte hoặc tệp đầy đủ nếu không có n được đưa ra.The split[] method creates a list from a string. The separator can be specified; the default separator is any whitespace.
Cú pháp: FileObject.Read [kích thước]
Split []: Phương thức chia [] tạo một danh sách từ một chuỗi. Phân tách có thể được chỉ định; Bộ phân cách mặc định là bất kỳ khoảng trắng nào. The replace[] method substitutes one phrase for another.
Cú pháp: String.Split [Dấu tách, MaxSplit]
Code:
Python3
Thay thế []: Phương thức thay thế [] thay thế một cụm từ khác.
Cú pháp: String.replace [trước_value, value_to_be_replace_with, đếm]
import json
with open[myfilename] as f:
lst = json.load[f]
3import json
with open[myfilename] as f:
lst = json.load[f]
4 import json
with open[myfilename] as f:
lst = json.load[f]
5import json
with open[myfilename] as f:
lst = json.load[f]
6import json
with open[myfilename] as f:
lst = json.load[f]
7import json
with open[myfilename] as f:
lst = json.load[f]
8import json
with open[myfilename] as f:
lst = json.load[f]
9with open[myfilename, "w"] as f:
json.dump[lst, f]
0>>> import pickle
>>> p= [[123231,2345,888754],[223467,85645]]
>>> with open["data.txt", "wb"] as internal_filename:
... pickle.dump[p, internal_filename]
3>>> import pickle
>>> p= [[123231,2345,888754],[223467,85645]]
>>> with open["data.txt", "wb"] as internal_filename:
... pickle.dump[p, internal_filename]
4>>> import pickle
>>> p= [[123231,2345,888754],[223467,85645]]
>>> with open["data.txt", "wb"] as internal_filename:
... pickle.dump[p, internal_filename]
5Output:
['Hello geeks Welcome to geeksforgeeks']
with open[myfilename, "w"] as f:
json.dump[lst, f]
1import json
with open[myfilename] as f:
lst = json.load[f]
4 with open[myfilename, "w"] as f:
json.dump[lst, f]
3onverting a text file into a
list by splitting the text on the occurrence of newline [‘\n’ ]
with open[myfilename, "w"] as f:
json.dump[lst, f]
import json
with open[myfilename] as f:
lst = json.load[f]
with open[myfilename, "w"] as f:
json.dump[lst, f]
with open[myfilename, "w"] as f:
json.dump[lst, f]
4import json
with open[myfilename] as f:
lst = json.load[f]
4 with open[myfilename, "w"] as f:
json.dump[lst, f]
6with open[myfilename, "w"] as f:
json.dump[lst, f]
7import json
with open[myfilename] as f:
lst = json.load[f]
8with open[myfilename, "w"] as f:
json.dump[lst, f]
9____________>>> import pickle
>>> p= [[123231,2345,888754],[223467,85645]]
>>> with open["data.txt", "wb"] as internal_filename:
... pickle.dump[p, internal_filename]
1with open[myfilename, "w"] as f:
json.dump[lst, f]
020Python3
Thay thế []: Phương thức thay thế [] thay thế một cụm từ khác.
Cú pháp: String.replace [trước_value, value_to_be_replace_with, đếm]
import json
with open[myfilename] as f:
lst = json.load[f]
3import json
with open[myfilename] as f:
lst = json.load[f]
4 import json
with open[myfilename] as f:
lst = json.load[f]
5import json
with open[myfilename] as f:
lst = json.load[f]
6import json
with open[myfilename] as f:
lst = json.load[f]
7import json
with open[myfilename] as f:
lst = json.load[f]
8import json
with open[myfilename] as f:
lst = json.load[f]
9with open[myfilename, "w"] as f:
json.dump[lst, f]
0>>> import pickle
>>> p= [[123231,2345,888754],[223467,85645]]
>>> with open["data.txt", "wb"] as internal_filename:
... pickle.dump[p, internal_filename]
3>>> import pickle
>>> p= [[123231,2345,888754],[223467,85645]]
>>> with open["data.txt", "wb"] as internal_filename:
... pickle.dump[p, internal_filename]
4>>> import pickle
>>> p= [[123231,2345,888754],[223467,85645]]
>>> with open["data.txt", "wb"] as internal_filename:
... pickle.dump[p, internal_filename]
5Output:
['Hello geeks', 'Welcome to geeksforgeeks']