如何从Python中的文件名中提取数据? - 将文件名转换为字符串?

发布于 2025-01-09 04:44:06 字数 380 浏览 0 评论 0原文

我正在尝试提取我在学校帮助进行的一些实验的元数据。我们将数据文件命名为如下所示:

name_date_sample_environment_run#.csv

我需要做的是编写一个函数,将每个部分分隔到一个列表中,该列表将输出如下:

['name', 'date', 'sample' , '环境', '运行#']

虽然我还没有完全弄清楚。我想我需要弄清楚如何加载文件,将名称转换为字符串,然后为每个下划线使用分隔符将每个下划线分隔到给定列表中。我不知道如何加载文件以便将其转换为字符串。任何帮助将不胜感激!

PS - 我最终需要找到一种方法将这些数据保存到电子表格中,这样我们就可以看到我们在某些条件下做了多少次实验,谁进行了这些实验,等等。但我可以稍后再弄清楚。谢谢!

I am trying to extract the meta data for some experiments I'm helping conduct at school. We are naming our data files something like this:

name_date_sample_environment_run#.csv

What I need to do is write a function that separates each piece to a list that'll be output like this:

['name', 'date', 'sample', 'environment', 'run#']

Though I haven't quite figured it out. I think I need to figure out how to load the file, convert the name to a string, then use a delimiter for each underscore to separate each into the given list. I don't know how to load the file so that I can convert it to a string. Any help will be appreciated!

P.S - I will eventually need to figure out a way to save this data into a spreadsheet so we can see how many experiments we do with certain conditions, who performed them, etc. but I can figure that out later. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬花如无物 2025-01-16 04:44:06

如果您只是问如何将字符串分解为用下划线分隔的所有组件,那么最简单的方法是使用 split 函数。

x = 'name_date_sample_environment_run#.csv'
y = x.split('_')

# y = ['name', 'date', 'sample', 'environment', 'run#.csv']

split 函数每次看到下划线时都会简单地分解字符串。如果您想从“run#.csv”中删除 .csv 部分,则可以处理原始字符串以删除最后 4 个字符。

x = 'name_date_sample_environment_run#.csv'
x = x[:-4]
y = x.split('_')

# y = ['name', 'date', 'sample', 'environment', 'run#]

If you're just asking how to break down the string into all the components separated by an underscore, then the easiest way would be using the split function.

x = 'name_date_sample_environment_run#.csv'
y = x.split('_')

# y = ['name', 'date', 'sample', 'environment', 'run#.csv']

The split function simply breaks down the string every time it sees the underscore. If you want to remove the .csv part from 'run#.csv' then you can process the original string to remove the last 4 characters.

x = 'name_date_sample_environment_run#.csv'
x = x[:-4]
y = x.split('_')

# y = ['name', 'date', 'sample', 'environment', 'run#]
_蜘蛛 2025-01-16 04:44:06

如果您的所有文件都是结构化的,并且在同一个文件夹中,您可以这样做:

import os 
files = os.listdir('.') #insert folder path

structured_files = {}
for file in files:
    name, date, sample, environment = file.split('_')
    structured_files.append({'name':name, 'date':date, 'sample':sample, 'env':env})

那么您将拥有一个包含文件信息的结构字典。
如果需要,您可以导入 pandas,然后保存到 Excel 工作表:

import os 
import pandas as pd

files = os.listdir('.') #insert folder path

structured_files = {}
for file in files:
    name, date, sample, environment = file.split('_')
    structured_files.append({'name':name, 'date':date, 'sample':sample, 'env':env})

pd.from_dict(structured_files).to_excel('files.xlsx')

If all your files are structured, and in the same folder you can do this way:

import os 
files = os.listdir('.') #insert folder path

structured_files = {}
for file in files:
    name, date, sample, environment = file.split('_')
    structured_files.append({'name':name, 'date':date, 'sample':sample, 'env':env})

Then you'll have a structure dict with your file info.
If you want to, you can import into pandas, and save to an excel sheet:

import os 
import pandas as pd

files = os.listdir('.') #insert folder path

structured_files = {}
for file in files:
    name, date, sample, environment = file.split('_')
    structured_files.append({'name':name, 'date':date, 'sample':sample, 'env':env})

pd.from_dict(structured_files).to_excel('files.xlsx')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文