[英]Calculate the difference of list elements if more than one element in the list is not zero
[英]Appending more than one element to the list
我想从文件中添加到空列表行,但是仅追加一个。 当我使用extend时,每个字母都会追加。 我希望每一行都是列表中的新元素。 如果我使用-如果打印的方法是所有方法,但仅附加最后一个方法。
如果我使用-如果打印的方法是所有方法,但仅附加最后一个方法。
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)
结果:
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']
如上所述,您应该在循环外初始化列表。 出于某种未知的原因,您正在使用“ else”打开“ if”条件...
尝试的例子..
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.