以下是一个示例代码,用于遍历文件中每行字符串的索引,这些字符串具有相同的长度,并从每个索引位置创建新的字符串值:
# 打开文件
with open('file.txt', 'r') as file:
# 读取文件的每一行
lines = file.readlines()
# 假设所有字符串具有相同的长度
str_length = len(lines[0].strip())
# 创建一个列表用于存储新的字符串值
new_strings = []
# 遍历每个索引位置
for i in range(str_length):
# 创建一个空的字符串
new_string = ''
# 遍历每一行字符串
for line in lines:
# 获取索引位置的字符
char = line[i]
# 将字符添加到新的字符串中
new_string += char
# 将新的字符串添加到列表中
new_strings.append(new_string)
# 打印新的字符串值
for new_string in new_strings:
print(new_string)
请注意,上述代码假设文件中的每一行字符串具有相同的长度,并且文件名为"file.txt"。您可以根据实际情况进行修改。