简体   繁体   English

如何读取名称存储在文本文件中的所有文件

[英]How to read all files that name store in text file

I am using python to read all find that path's of file store in text file such as (name_path.txt)我正在使用 python 读取所有在文本文件中找到的文件存储路径,例如 (name_path.txt)

0_7\069A07.JPG;0
0_7\070A02.JPG;0
0_7\070A03.JPG;0
20_36\003A25.JPG;1
20_36\003A35.JPG;1
20_36\057A23.JPG;1
20_36\057A25.JPG;1

In which first element is path of file, and second element (seperated by ";") store label.其中第一个元素是文件路径,第二个元素(以“;”分隔)存储标签。 Now I want to use python to get:现在我想使用 python 来获取:

  1. File path such as 0_7\\069A07.JPG and its label such as 0. (see in first row)文件路径如0_7\\069A07.JPG及其标签如 0。(见第一行)

Could you help me to implement it?你能帮我实现它吗? Thanks you so much Update This is what I am doing非常感谢你更新这就是我在做什么

infile = "name_path.txt"
data = open(infile, "r").readlines()
# Create a dictionary list
for line in data:
    row = line.split(";");
    pathname=row[0]
    label=row[1]

You can use a dictionary to store your labels as the key and the paths in a list :您可以使用字典将标签存储为列表中的键和路径:

infile = "name_path.txt"
d={}
with open(infile, "r") as f:
  for line in f:
    path,label = line.split(";")
    d.setdefault(label,[]).append(path)

setdefault(key[, default]) setdefault(key[, 默认])

If key is in the dictionary, return its value.如果键在字典中,则返回其值。 If not, insert key with a value of default and return default.如果没有,插入值为 default 的键并返回默认值。 default defaults to None.默认默认为无。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从具有相同名称结构的文本文件中读取数据,并将所有数据附加到新文件中 - Read data from text files having same name structure and append all data into a new file 如何读取Github存储库中的所有文本文件? - How to read all text files in Github repository? 如何读取子文件夹中的所有文本文件并将文件内容(四位数字)添加到列表中? - How to read all the text files in the subfolders and add the file content (a four digit number) to a list? 如何使用tesseract python 3读取目录中的所有pdf文件并转换为文本文件? - How to read all pdf files in a directory and convert to text file using tesseract python 3? 读取文本文件并存储到字典中 - Read text file and store into dictionary 如何从文本文件读取并将多个值存储到字典中的键 - How to read from a text file and store multiple values to a key in dictionary 如何读取随机导入的文本文件并将其存储在列表中 - How to read a randomly imported text file and store it in a list 如何读取文本文件并在python中存储为不同的变量 - How to read a text file and store as different variables in python 如何从文本文件读取值并将其存储到字典中。 [蟒蛇] - How to read and store values from a text file into a dictionary. [python] 读取文件夹中的所有 excel 文件并拆分每个文件名,将拆分后的名称添加到 dataframe - Read the all excel files in a folder and split the each file name, add splitted name into the dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM