1

I have to change the res variable value in the next code for every loop it does.

txt = open(os.path.expanduser('~FOLDER\\numbers.txt'), 'r')
res = txt.read().splitlines()

u = [something]    
for item in u:
    var['Number : ' + res[0]]

txt variable contains a text file. In this text file there some lines of numbers in this format:

123
1234
125342
562546

I have to take a variable for each loop the script does and assign to res. At the moment, with res[0] it only iterate the same number (ex: 123) on every loop. How can I solve the problem ? It should be 0 at first, 1 at second ad so on...

lucians
  • 2,239
  • 5
  • 36
  • 64

3 Answers3

1

I think this should do the job :

with open(os.path.expanduser('~FOLDER\\numbers.txt'), 'r') as res: for line in res: var['Number ': line]

More info here

Romain B.
  • 656
  • 6
  • 15
0
txt = open(os.path.expanduser('~FOLDER\\numbers.txt'), 'r')
res = txt.read().splitlines()

u = [something]    
for index, item in enumerate(u):
    var['Number : ' + res[index]]

More info about enumerate here: https://docs.python.org/2/library/functions.html#enumerate

StefanE
  • 7,578
  • 10
  • 48
  • 75
0

I assume you want to iterate through u and v simultaneously. In this case, you either want to just use a plain index using for loop over a range, or you could use enumerate as follows:

txt = open(os.path.expanduser('~FOLDER\\numbers.txt'), 'r')
res = txt.read().splitlines()

u = [something]    
for index, item in enumerate(u):
    var['Number : ' + res[index]]
Eric Zhang
  • 591
  • 6
  • 13