英文:
Python increase parameter number in loop
问题
我现在也想遍历“TEXT_1”,因为还有“TEXT_2”,“TEXT_3”等,它们都应该从dataframe1.iloc[1,1+i]获取不同的值。但我不知道如何遍历TEXT_n。你能帮助我吗?
非常感谢,
Jerry
英文:
I have a text file where I want to replace some placeholder strings with data from an Excel file. Therefore I created this code:
import pandas as pd
with open('testfile_1.txt', 'r') as file :
filedata = file.read()
dataframe1 = pd.read_excel('EXCEL_Data.xlsx', header=None)
values = range(5)
for i in values:
filedata = filedata.replace('TEXT_1', dataframe1.iloc[1,1+i])
I now also want to iterate through "TEXT_1" since there is "TEXT_2", "TEXT_3", etc. which should all get different values from dataframe1.iloc[1,1+i]
But I don`t know how to iterate through TEXT_n. Can you please help me?
Thank you very much,
Jerry
答案1
得分: 0
如果你需要 'TEXT_i',你可以使用字符串拼接:'TEXT_' + str(i + 1)。另一种方法是使用字符串插值:f'TEXT_{i + 1}'。这两种方法都会给你 'TEXT_1'、'TEXT_2'、...、'TEXT_5'。
英文:
If you need 'TEXT_i', you can use string concatenation: 'TEXT_' + str(i + 1)
. Other way is to use string interpolation: f'TEXT_{i + 1}'
. Both methods will give you 'TEXT_1', 'TEXT_2', ..., 'TEXT_5'.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论