从字符串中删除数字

问题描述 投票:0回答:1

有一个包含数字列表的文件。 示例:

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()
python string list file
1个回答
0
投票

几乎正确,但不完全正确😅:

  1. 您正在使用
    str
    作为变量名称。
    str
    是Python内置函数名称,请使用其他名称以避免混淆。
  2. numbers
    定义于 if 语句的
    内部
    。如果在下一个
    if
    语句中使用它可能不会被定义。
  3. 第二个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()
© www.soinside.com 2019 - 2024. All rights reserved.