有一个包含数字列表的文件。 示例:
AK
v 0.00o 0.00 0.00 0.00 0.00 0.00 0.00
750.00 0.00 0.00 0.00 0.00 0.00 0.00
750.00 43.00 0.00 0.00 0.00 0.00 0.00
585.00 43.00 -7.00 0.00 0.00 0.00 0.00
-16611181.71 -90.16t -7.00 0.00 0.00 0.00 0.00
585.00 57.00 0.00 0.00 0.00 0.00 0.00
750.00 57.00 0.00 0.00 0.00 0.00 0.00
750.00 100.00 0.00 0.00 0.00 0.00 0.00
0.00 100.00 0.00 0.00 0.00 0.00 0.00
0.00 56.99 0.00 0.00 0.00 0.00 0.00
165.00 56.99 -7.00 0.00 0.00 0.00 0.00
16612362.17 344.30t -7.00 0.00 0.00 0.00 0.00
165.00 42.99 0.00 0.00 0.00 0.00 0.00
0.00 42.99 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00
这是编辑文件后应该得到的:
AK
v 0.00o 0.00 0.00 0.00 0.00 0.00 0.00
750.00 0.00 0.00 0.00 0.00 0.00 0.00
750.00 43.00 0.00 0.00 0.00 0.00 0.00
585.00 43.00 -7.00 0.00 0.00 0.00 0.00
585.00 57.00 0.00 0.00 0.00 0.00 0.00
750.00 57.00 0.00 0.00 0.00 0.00 0.00
750.00 100.00 0.00 0.00 0.00 0.00 0.00
0.00 100.00 0.00 0.00 0.00 0.00 0.00
0.00 56.99 0.00 0.00 0.00 0.00 0.00
165.00 56.99 -7.00 0.00 0.00 0.00 0.00
165.00 42.99 0.00 0.00 0.00 0.00 0.00
0.00 42.99 0.00 0.00 0.00 0.00 0.00
0.00 0.00 0.00 0.00 0.00 0.00 0.00
您需要从中删除整行,其中数字> 12000或数字< -12000. I tried to split the string into substrings and compare the value, but nothing worked :(
import PySimpleGUI as sg
import os
sg.theme('Default1')
OUTFILE = 'tmp.nc1'
layout = [
[sg.InputText(), sg.FileBrowse('...', key = '-FILE-') ],
[sg.Button('Исправить')]
]
window = sg.Window('Исправление позиций', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'Исправить':
with open(values['-FILE-']) as infile, open(OUTFILE, 'w') as outfile:
for line in infile:
if line.startswith(' '):
str = line.split()
numbers = int(str[0])
if not (numbers < 12000) or not (numbers > -12000):
outfile.write(line)
os.remove(values['-FILE-'])
os.rename(OUTFILE, values['-FILE-'])
window.close()
几乎正确,但不完全正确😅:
str
作为变量名称。 str
是Python内置函数名称,请使用其他名称以避免混淆。numbers
定义于 if
语句的内部。如果在下一个
if
语句中使用它可能不会被定义。if
中的条件是不正确:应该是
and
而不是or
。这是更正后的代码:
import PySimpleGUI as sg
import os
sg.theme('Default1')
OUTFILE = 'tmp.nc1'
layout = [[sg.InputText(), sg.FileBrowse('...', key = '-FILE-')],[sg.Button('Исправить')]]
window = sg.Window('Исправление позиций', layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == 'Исправить':
with open(values['-FILE-']) as infile, open(OUTFILE, 'w') as outfile:
for line in infile:
if line.startswith(' '):
str_line = line.split()
numbers = float(str_line[0])
if numbers > -12000 and numbers < 12000:
outfile.write(line)
os.remove(values['-FILE-'])
os.rename(OUTFILE, values['-FILE-'])
window.close()