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
- Một từ điển trong Python là bộ sưu tập các cặp giá trị khóa, trong đó khóa luôn luôn độc đáo và thường thì chúng ta cần lưu trữ từ điển và đọc lại.
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
2 method : Converts the string of valid dictionary into json form. - Chúng ta có thể đọc một từ điển từ một tệp theo 3 cách:
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
3 method : Function safer than the eval function and can be used for interconversion of all data types other than dictionary as well. - Sử dụng phương thức
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
2: Chuyển đổi chuỗi từ điển hợp lệ thành biểu mẫu JSON.Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
4 method : We can also use Pickle module if the file is serialized into a character stream.
Sử dụng phương pháp Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
3: Hàm an toàn hơn hàm đánh giá và có thể được sử dụng để xen kẽ tất cả các loại dữ liệu khác ngoài từ điển.
Sử dụng phương thức Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
4: Chúng ta cũng có thể sử dụng mô -đun Pickle nếu tệp được tuần tự hóa thành luồng ký tự. Using Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
2 :
Tệp đầu vào:
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}8
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}9
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}0
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}1
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}2
Phương pháp 1: Sử dụng
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}2:
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}6
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}7
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}3
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}4
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}5
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}6
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}7
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}0
4 x
5 y
6 z
84 x
5 y
6 z
04 x
5 y
6 z
1a = {}
with open["File1.txt"] as f:
for line in f:
[k, v] = line.split[]
a[int[k]] = v
print[a]
1Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}7
a = {}
with open["File1.txt"] as f:
for line in f:
[k, v] = line.split[]
a[int[k]] = v
print[a]
3Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
7________L
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
34 x
5 y
6 z
Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
5
5 Using 4 x
5 y
6 z
Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
3 :
Đầu ra:
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}8
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}9
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}0
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}1
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}2
Phương pháp 1: Sử dụng
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}2:
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}7
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}0
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}9
4 x
5 y
6 z
04 x
5 y
6 z
14 x
5 y
6 z
2Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}6
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}7
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}3
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}4
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}5
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}6
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}7
{4: 'x', 5: 'y', 6: 'z'}
2Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
7________L
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
34 x
5 y
6 z
Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
5
5 We can use the Pickle module for the same purpose, but this method will only work if the file is serialized into a character stream and not in text format. To know more about Pickling in Python click here4 x
5 y
6 z
Đầu ra:
Phương pháp 2: Sử dụng
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}3:
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}6
a = {}
with open["File1.txt"] as f:
for line in f:
[k, v] = line.split[]
a[int[k]] = v
print[a]
6{4: 'x', 5: 'y', 6: 'z'}
0{4: 'x', 5: 'y', 6: 'z'}
1with open["File1.txt"] as f:
a = {int[k]: v for line in f for [k, v] in [line.strip[].split[None, 1]]}
print[a]
7{4: 'x', 5: 'y', 6: 'z'}
3with open["File1.txt"] as f:
a = {int[k]: v for line in f for [k, v] in [line.strip[].split[None, 1]]}
print[a]
9{4: 'x', 5: 'y', 6: 'z'}
0{4: 'x', 5: 'y', 6: 'z'}
6with open["File1.txt"] as f:
a = {int[k]: v for line in f for [k, v] in [line.strip[].split[None, 1]]}
print[a]
7{4: 'x', 5: 'y', 6: 'z'}
8{4: 'x', 5: 'y', 6: 'z'}
9Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}00
{4: 'x', 5: 'y', 6: 'z'}
5with open["File1.txt"] as f:
a = {int[k]: v for line in f for [k, v] in [line.strip[].split[None, 1]]}
print[a]
2{4: 'x', 5: 'y', 6: 'z'}
5Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}04
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}8
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}9
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}0
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}1
4 x
5 y
6 z
0Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}10
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}11
with open['File1.txt'] as f:
a = dict[i.rstrip[].split[None, 1] for i in f]
print[a]
2Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}5
with open['File1.txt'] as f:
a = dict[i.rstrip[].split[None, 1] for i in f]
print[a]
4Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}7
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}0
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}9
4 x
5 y
6 z
04 x
5 y
6 z
14 x
5 y
6 z
2Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}7________L
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}3
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}4
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}5
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}6
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}7
{4: 'x', 5: 'y', 6: 'z'}
2Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
7________L
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
Để truy cập giá trị từ điển trong Python, bạn có ba tùy chọn:. Sử dụng từ điển hiểu để đọc một tệp vào từ điển trong Python Hướng dẫn này cho thấy các cách khác nhau để đọc một tệp vào một từ điển trong Python. Để tham khảo, chúng tôi sẽ sử dụng một tệp văn bản trong mã để giải thích các phương thức khác nhau được sử dụng trong bài viết. Nội dung của tệp Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
34 cùng với hàm Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
33 để đọc tệp vào từ điển trong PythonData type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
34 cùng với hàm Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
33 để đọc tệp vào từ điển trong PythonData type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
33 tại ký tự không gian. Hàm Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
34 cũng được sử dụng trong cùng một ký tự đã đề cập.Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
37:
4 x
5 y
6 z
Sử dụng chức năng Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
33 để đọc tệp vào từ điển trong Python
Hàm
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}33 thường được sử dụng để cắt một chuỗi đã cho vào danh sách.
Mã sau sử dụng hàm
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}33 để đọc một tệp vào từ điển trong Python.
a = {}
with open["File1.txt"] as f:
for line in f:
[k, v] = line.split[]
a[int[k]] = v
print[a]
Mã trên cung cấp đầu ra sau:
{4: 'x', 5: 'y', 6: 'z'}
Explanation:
- Một từ điển trống
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
41 được tạo đầu tiên. - Hàm
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
42 được sử dụng để mở và đọc từ tệp đã choData type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
37 - Nội dung của tệp được đọc từng dòng.
- Các nội dung dòng sau đó được cắt nhỏ bằng hàm
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
33 tại ký tự không gian. Ký tự trước không gian được lấy làm chìa khóa trong khi ký tự sau không gian được lấy làm giá trị của từ điển. - Vòng lặp
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
45 được sử dụng cho mục đích lặp và để đạt đến phần cuối của tệp.
Sử dụng chức năng Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
34 cùng với hàm Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
33 để đọc tệp vào từ điển trong Python
Hàm
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}34 trong Python sẽ loại bỏ bất kỳ ký tự hoặc khoảng trống đặc biệt nào ở đầu và cuối chuỗi. Hàm trả về một chuỗi mới thay vì thực hiện các thay đổi cho chuỗi gốc.
Mã sau sử dụng hàm
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}34 và hàm
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}33 để đọc một tệp vào từ điển trong Python.
with open['File1.txt'] as f:
a = dict[i.rstrip[].split[None, 1] for i in f]
print[a]
Mã trên cung cấp đầu ra sau:
Một từ điển trốngData type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}41 được tạo đầu tiên.
Explanation:
- Một từ điển trống
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
41 được tạo đầu tiên. - Hàm
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
42 được sử dụng để mở và đọc từ tệp đã choData type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
37 - Nội dung của tệp được đọc từng dòng.
- Các nội dung dòng sau đó được cắt nhỏ bằng hàm
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
33 tại ký tự không gian. Ký tự trước không gian được lấy làm chìa khóa trong khi ký tự sau không gian được lấy làm giá trị của từ điển. - Vòng lặp
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}
45 được sử dụng cho mục đích lặp và để đạt đến phần cuối của tệp.
Sử dụng chức năng Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
34 cùng với hàm Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
33 để đọc tệp vào từ điển trong Python
Hàm
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}34 trong Python sẽ loại bỏ bất kỳ ký tự hoặc khoảng trống đặc biệt nào ở đầu và cuối chuỗi. Hàm trả về một chuỗi mới thay vì thực hiện các thay đổi cho chuỗi gốc.
Mã sau sử dụng hàm
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}34 và hàm
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}33 để đọc một tệp vào từ điển trong Python.
{4: 'x', 5: 'y', 6: 'z'}
with open["File1.txt"] as f:
a = {int[k]: v for line in f for [k, v] in [line.strip[].split[None, 1]]}
print[a]
Mã trên cung cấp đầu ra sau:
{4: 'x', 5: 'y', 6: 'z'}
Một từ điển trống Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
41 được tạo đầu tiên.
Hàm
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}42 được sử dụng để mở và đọc từ tệp đã cho
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}37
Nội dung của tệp được đọc từng dòng.
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}0
Mã trên cung cấp đầu ra sau:
{4: 'x', 5: 'y', 6: 'z'}
Một từ điển trống Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
41 được tạo đầu tiên.
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}42 được sử dụng để mở và đọc từ tệp đã cho
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}37
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}33 tại ký tự không gian. Ký tự trước không gian được lấy làm chìa khóa trong khi ký tự sau không gian được lấy làm giá trị của từ điển.
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}45 được sử dụng cho mục đích lặp và để đạt đến phần cuối của tệp.
Sử dụng chức năng Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
34 cùng với hàm Data type before reconstruction :
Data type after reconstruction :
{'Name': 'John', 'Age': 21, 'Id': 28}
33 để đọc tệp vào từ điển trong Python
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}34 trong Python sẽ loại bỏ bất kỳ ký tự hoặc khoảng trống đặc biệt nào ở đầu và cuối chuỗi. Hàm trả về một chuỗi mới thay vì thực hiện các thay đổi cho chuỗi gốc.
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}33 tại ký tự không gian. Ký tự trước không gian được lấy làm chìa khóa trong khi ký tự sau không gian được lấy làm giá trị của từ điển.
Data type before reconstruction : Data type after reconstruction : {'Name': 'John', 'Age': 21, 'Id': 28}45 được sử dụng cho mục đích lặp và để đạt đến phần cuối của tệp.