可以使用.txt文件名作为Python字典的键吗?

huangapple go评论56阅读模式
英文:

Can I use .txt filenames "dynamically" as Keys for a python dictionary?

问题

抱歉,我的英语不太好,请谅解。

其次,我能否创建一个字典,使其“动态”地使用我想要读取/打开的.txt文件的“结尾”作为字典的键?

背景是,我做了很多测量,但忘记在.txt文件中添加某个变量。这些.txt文件有时包含超过5000行(例如时间测量)。

实际上,我只忘记了13个不同的值。.txt文件都以某个计划的名称命名,因此结尾"_12"或"_01"将始终是我在脚本中用于计算的相同值。

目前的问题是,我不太清楚如何正确地将键连接到文件名,以及如何将字典“转换”为“适当”的值(出现了TypeError:unsupported operand type(s) for *: 'int' and 'dict')。尽管我认为后面的问题可以通过创建一个元组来解决?

非常感谢大家的帮助。

我首先尝试创建一个简单的字典,然后使用简单的“J=I*A”,理想情况下,A将是根据文件名从数据框中获取的某个变量的值。

英文:

First of all, sorry for my bad englisch - please bear with me.

Secondly; can I create a dictionary so "dynamically", that I can use the "Endings" of the .txt Files I want to read/open with the script as keys for a dictionary?

The Background is; I've made alot of measurments, yet I forgot to put in a certain Variable in the .txt Files. Those .txt files though do contain sometimes over 5000 lines (Time measurments e.g.).

Yet I've actually only 13 different values I forgot. The .txt Files are all named after a certain plan, so the Ending _12 or _01 will be always the same value I miss for a calculation in the script.

The issue right now is, that I don't exactly know how connect the key to the file name properly and how to "convert" the Dictionary to a "proper" value. (Getting the TypeError: unsupported operand type(s) for *: 'int' and 'dict') - Although I think the later problem could be solved by creating a tuple?

Thank y'all in advance.

I tried first to create a simple dict and then use simple "J=I*A", ideally A would be a certain variable from the dictionary, depending on the Filename the I is from via Dataframe.

答案1

得分: 0

你显然已经在你的程序中打开文件,所以你应该能够根据以下内容来获取你想要的文件值:

filename = "relative_or_abs_path_to_your_file_01.txt"

# 返回一个列表,包含扩展名 ('txt') 作为元素 1,文件名的其余部分作为元素 0
f = filename.rsplit('.', 1)

# 在元素 0 (文件名元素) 上拆分以获取下划线后的值 (返回列表的元素 1)
missing_value = f[0].rsplit('_', 1)[1]

missing_value = int(missing_value)  # 转换为整数
print(missing_value)

一旦你有了 missing_value 变量,你可以将它放入字典或任何你需要的地方,以适应你的其余代码。 (或者直接按原样使用它。)

英文:

You're obviously already opening the file(s) in your program, so you should be able to adapt the following to get the value you want from the file in question:

filename = "relative_or_abs_path_to_your_file_01.txt"

# Returns a list with extension ('txt') as element 1, and the rest of the filename as element 0
f = filename.rsplit('.', 1)

# Split on element 0 (the filename element) to get value after the underscore (element 1 of the returned list)
missing_value = f[0].rsplit('_', 1)[1]

missing_value = int(missing_value)  # Convert into an integer
print(missing_value)

Once you have the missing_value variable, you can obviously put it into a dict, or wherever you need it, to accommodate the rest of your code. (Or just use it directly as-is.)

huangapple
  • 本文由 发表于 2023年2月26日 19:54:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定