I want to add to the empty list lines from the file but only one is appending. When I use extend, every single letter is appending. I want that every line is the new element in the list. If i use for - if method printed is everything but appended only the last one.
If i use for - if method printed is everything but appended only the last one.
with open ("sesja1_1101_1.19o", "r", encoding = "UTF-8") as file:
isIn = file.readlines()
for line in isIn:
elif "PRN / # OF OBS" in line:
line = line.replace("\n", "")
line = line.replace("PRN / # OF OBS", "")
line = line.lstrip()
line = line.rstrip()
sattelites = []
sattelites.append(line)
print (line)
result:
runfile('C:/Users/HP/Desktop/mgr/mgr.py', wdir='C:/Users/HP/Desktop/mgr')
G05 729 0 688 400 400
G07 622 0 595 307 307
G08 486 0 455 259 259
G11 220 0 194 83 83
G13 1149 0 1149 1149 1149
G15 929 0 916 851 851
G17 378 0 365 261 261
G20 59 0 41 0 0
G24 510 0 495 465 465
G28 1149 0 1149 1149 1149
G30 1149 0 1149 1149 1149
R01 311 308 310 582 279
R07 1148 1148 1148 2294 1148
R08 1149 1149 1149 2298 1149
R09 1149 1149 1149 2298 1149
R10 1149 1149 1149 2298 1149
R11 357 357 357 712 355
R16 250 232 236 454 230
R17 6 1 4 1 0
R18 1113 1095 1112 2164 1089
R19 234 227 231 449 226
sattelites
Out[16]: ['R19 234 227 231 449 226']
as commented above, you should initialize your list outside of the loop. and for some unknown reason you're opening an 'if' condition with 'else'...
example to try..
sattelites = []
with open ("open_this", "r", encoding = "UTF-8") as file:
isIn = file.readlines()
for line in isIn:
if "this_exists" in line: # <-- change 'elif' to 'if'
new_line = line
new_line = new_line.replace('replace/remove').lstrip('some').rstrip('stuff')
sattelites.append(new_line)
print(new_line)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.