如何使用循环为从 csv 导入的多个相似列创建名称相似的字符串?

How can I use a loop to create similarly-named strings for a number of similar columns imported from a csv?

我想在 python 中使用 csv 格式的数据。我想让每一列成为一个单独的字符串,我想知道是否有一种方法可以遍历这个过程,这样我就不必单独指定每个字符串的名称(因为命名约定非常相似) .

对于一些 csv 列,我使用以下代码:

    dot_title=str(row[0]).lower()
    onet_title=str(row[1]).lower()

对于[2]-[11],我希望每个字符串的名称相同但编号。即,row[2] 将成为一个名为 onet_reported_1 的字符串,row[3] 将成为 onet_reported_2,row[4] 将成为 onet_reported_3... 等等,一直到行[12].

有没有办法通过循环来完成此操作,而不是简单地单独定义 onet_reported_1、_2、_3、_4 等?

提前致谢!

所以,首先要弄清楚一些。

字符串是变量类型。在 Python 中,您通过将一些文本括在单引号或双引号中来创建一个字符串。

"This is a string"
'So is this. It can have number characters: 123. Or any characters: !@#$'

字符串是可以分配给变量的值。所以你使用一个字符串给它一个名字:

my_string = "This is a string"
another_string = "One more of these"

您可以对字符串执行不同类型的操作,例如使用 + 运算符

连接它们
new_string = my_string + another_string

并且您可以创建字符串列表:

list_of_strings = [new_string, my_string, another_string]

看起来像 ["This is a stringOne more of these", "This is a string", "One more of these"]

要在循环中创建多个字符串,您需要一个地方来存储它们。列表是一个不错的选择:

list_of_strings = []
for i in range(1, 11):
    list.append("onet_reported_" + i)

但我 认为 你想要的是将变量命名为“onet_reported_x”,这样你最终得到的结果相当于 :

onet_reported_1 = row[1] 
onet_reported_2 = row[2] 

等等,无需输入所有冗余代码。这是一个很好的直觉。做这种事情的一个好方法是创建一个字典,其中键是您想要的字符串名称,值是 row[i]。您可以循环执行此操作:

onet_dict = {}
for i in range(1, 11):
    onet_dict["onet_reported_" + i] = row[i]

或通过字典理解:

onet_dict = {"onet_reported_" + i: row[i] for i in range(1,11)}

两者都会给你相同的结果。现在你有了一个字符串集合,其中包含你想要的名称作为映射到你希望它们关联的行值的字典的键。要使用它们,而不是直接引用名称 onet_reported_x 您必须从字典中访问值,例如:

# Adding some other value to onet_reported_5. I'm assuming the values are numbers.

onet_dict["onet_reported_5"] += 20457