简体   繁体   English

Python:如何基于给定文件夹的内容创建具有名称的列表?

[英]Python: how to create a list with names based on the content of a given folder?

I need to create a list like this: myindex=['event 1','event 2','event 3', ..., 'event n'] , where n is the number of csv files I have in a given directory. 我需要创建一个像这样的列表: myindex=['event 1','event 2','event 3', ..., 'event n'] ,其中n是给定的csv文件数目录。

To implement this I wrote the following: 为了实现这一点,我编写了以下内容:

directoryPath=raw_input('Directory for csv files: ')
myindex=[] #'event 1','event 2','event 3','event 4','event 5','event 6'
for i,file in enumerate(os.listdir(directoryPath)):
    if file.endswith(".csv"):
        myindex.append('event '+str(i))

The directory I pass it has n csv files and n jpg files, but I only want to consider the former. 我通过的目录有n csv文件和n jpg文件,但我只想考虑前者。

The result I get: myindex=['event 0','event 2', 'event 4', ..., 'event 2(n-1)'] . 我得到的结果是: myindex=['event 0','event 2', 'event 4', ..., 'event 2(n-1)']

The result I want: myindex=['event 1', 'event 2', 'event 3', 'event 4', ..., 'event n'] 我想要的结果是: myindex=['event 1', 'event 2', 'event 3', 'event 4', ..., 'event n']

What should I amend in my block? 我应该在区块中修改什么? Thanks! 谢谢!

EDIT 编辑

What if I want the first element of myindex to be an empty string ? 如果我希望myindex的第一个元素为空string怎么办? So to have myindex=['','event 1','event 2',...,'event n'] . 因此具有myindex=['','event 1','event 2',...,'event n']

enumerate takes a second parameter that is the number to start with. enumerate采用第二个参数,即开头的数字。 Use 1 . 使用1 Then use glob to count only csv files: 然后使用glob仅计算csv文件:

for i,v in enumerate(glob.glob('*.csv'),1):

Use a genex to filter the filenames. 使用Genex过滤文件名。

enumerate((x for os.listdir(directoryPath) if x.endswith(".csv")), start=1)

Or have the OS do it. 或者让操作系统来做。

enumerate(glob.glob(os.path.join(directoryPath, "*.csv")), start=1)

You need a counter instead of using enumerate since that increments for every loop 您需要一个计数器而不是使用枚举,因为每个循环都会增加

directoryPath=raw_input('Directory for csv files: ')
myindex=[''] #'event 1','event 2','event 3','event 4','event 5','event 6'

count = 0
for file in os.listdir(directoryPath):
    if file.endswith(".csv"):
        count += 1
        myindex.append('event '+str(count))

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

相关问题 给定Python中的变量名称列表,如何创建一个变量名称为键的字典(对于变量的值)? - Given a list of variable names in Python, how do I a create a dictionary with the variable names as keys (to the variables' values)? 如何在文件夹中创建图像列表及其名称 - How to create a list of images and their names in a folder 希望根据列表中的名称创建团队生成器 - Python - Looking to create a team generator based on names in a list- Python 根据表中给定的数据创建一个新的嵌套列表/元组 [Python] - Create a new nested list / tuple based on the given data in the table [Python] 使用Python导入文件夹中的文件夹名称列表 - Import list of folder names in a folder with Python 如何使用 python 按字母顺序排列文件夹名称列表 - How can I order a list of folder names alphabetically using python 如何根据内容在Python中对列表进行重新排序 - How to reorder a list in Python based on its content 如何基于包含列名的列表创建数据框? - How to create a dataframe based on list containing column names? Python:给定两个文件名列表,根据日期部分找到常见的文件名 - Python: Given two list of file names find common ones based on date part Python-根据参数值创建文件夹名称 - Python - Creating folder names based on argument values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM