0

Python: how I could access to item values in list?

Hello all, I read TXT file trought a list. But how I could access to item values in the list? Thank you in advance for answers! Code: with open( dat, "r", encoding="ANSI" ) as f: lines=f.read().split() print( lines.__str__()) f.close() The result is: ['""', '""', '""', '""', '""', '""', '"Interval:', '01/01/17', '00:00:00', '-', '31/01/17', '23:59:59"', '""']

3rd Nov 2017, 1:15 PM
Radostina Stefanova
Radostina Stefanova - avatar
2 ответов
+ 1
If you want to access (lines) outside the with cycle, you need to declare the variable before the loop. lines = [] with open(dat, "r", encoding="ANSI") as f: lines = f.read().split() print(lines.__str__()) f.close() for elem in lines: print(elem) print(lines[0])
3rd Nov 2017, 1:45 PM
Eugene Fedorov
Eugene Fedorov - avatar
0
thanks a lot, Eugene!!!!!
3rd Nov 2017, 2:47 PM
Radostina Stefanova
Radostina Stefanova - avatar