如何从Python中的文件名中提取数据? - 将文件名转换为字符串?
我正在尝试提取我在学校帮助进行的一些实验的元数据。我们将数据文件命名为如下所示:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只是问如何将字符串分解为用下划线分隔的所有组件,那么最简单的方法是使用 split 函数。
split 函数每次看到下划线时都会简单地分解字符串。如果您想从“run#.csv”中删除 .csv 部分,则可以处理原始字符串以删除最后 4 个字符。
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.
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.
如果您的所有文件都是结构化的,并且在同一个文件夹中,您可以这样做:
那么您将拥有一个包含文件信息的结构字典。
如果需要,您可以导入 pandas,然后保存到 Excel 工作表:
If all your files are structured, and in the same folder you can do this way:
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: