use pythonread a txt file and save the content as a dict
Assuming the txt file has a key-value pair on each line separated by a colon, like this:
key1:value1
key2:value2
key3:value3
Here's a Python code to read the file and store the content as a dictionary:
filename = "file.txt"
my_dict = {}
with open(filename, "r") as file:
for line in file:
key, value = line.strip().split(":")
my_dict[key] = value
print(my_dict)
This code opens the file in read mode, loops through each line, extracts the key-value pair by splitting on the colon, and adds it to the dictionary. Finally, it prints the dictionary.
原文地址: https://www.cveoy.top/t/topic/bVT5 著作权归作者所有。请勿转载和采集!