Put in the ".txt" file the information contained in line 2 [0: 6] [closed]

-2

Put in the ".txt" file the information contained in line 2 [0: 6] which is:

  

['Memories', 'physics', 'total:', '7.988', 'MB']
  ['Memoir', 'physics', 'available:', '5.277', 'MB']
  ['Memory', 'Virtual:', 'Size', 'M', ':', '9.268']
  ['Memory', 'Virtual:', 'Available:', '6.094', 'MB']
  ['Memory', 'Virtual', 'Em', 'Usage:', '3.174', 'MB']

archive_sysinfo = open('saida_systeminfo.txt', 'r')
text_sysinfo = archive_sysinfo.readlines()
lista_linhas2 = []
lista_info = []

for linha2 in text_sysinfo:
    lista_linhas2.append(linha2.split())

for linha2 in lista_linhas2:
    if "Mem¢ria" in linha2:
        lista_info.append(linha2[0:6])
        print(linha2[0:6])

OUTPUT:

  

Host Name: FPAL169325
  Name of the operating system: Microsoft Windows 8.1 Pro
  Version of the operating system: 6.3.9600 N / A build 9600
  Operating System Manufacturer: Microsoft Corporation
  OS configuration: Member workstation
  Operating system compile type: Multiprocessor Free
  Registered owner: FACULDADE SENAC PORTO ALEGRE
  Registered organization: SENACRS
  Product Identification: 00261-80511-41292-AA269
  Date of original installation: 08/01/2018, 19:29:13
  System Initialization Time: 10/26/2018, 20:47:23
  System manufacturer: LENOVO
  System Model: 32092F2
  System Type: x64-based PC
  Processor (s): 1 processor (s) installed.                                               [01]: Intel64 Family 6 Model 58
  Stepping 9 GenuineIntel ~ 3201 Mhz
  BIOS Version: LENOVO 9SKT69AUS, 05/17/2013
  Windows folder: C: \ windows
  System folder: C: \ windows \ system32
  Initialize Device: \ Device \ HarddiskVolume1
  Location of the system: pt-br; Portugus (Brasil)
  Place of entry: pt-br; Portugus (Brazil)
  Time zone: (UTC-03: 00) Brasília
  Total memory: 7.988 MB
  Physical memory available: 5,277 MB
  Virtual Memory: Maximum Size: 9.268 MB
  Virtual Memory: Available: 6.094 MB
  Virtual Memory: In Use: 3.174 MB
  Page File Location (s): C: \ pagefile.sys
  Domínnia: fspoaeduc.com.br
  Logon Server: \ CROM
  Hotfix (s): 165 hotfix (s) installed.                                              [01]: KB2899189_Microsoft-Windows-CameraCodec-Package

    
asked by anonymous 30.11.2018 / 12:44

1 answer

4

Since you want to copy part of a file to another, the steps I would take are:

  • Open a file for reading ( open('meu_arquivo', 'r') )
  • Open a file for recording ( open('outro_arquivo', 'w') )
  • Read the file line by line for linha in arquivo:
  • Test whether the line being read should be copied (in this case I used str.startswith() )
  • Write the line in the output file (See file-objects methods ).
  • I advise you to open files with with , so he will close the file at the end of the reading or in case of an error occur.

    Below is a working example:

    with open('saida_systeminfo.txt', 'r') as info, \
         open('resultado.txt', 'w') as txt:
        for line in info:
            if line.startswith('Memória'):
                txt.write(line)
    

    Repl.it with the working code

    Edit

    Since the content of the file that the AP posted contains some encoding errors, I'll just point out that the open() has an optional encoding parameter where you can use one of encodings supported by python . Examples:

    arquivo = open('meu_arquivo.txt', 'r', encoding='utf_8')
    arquivo = open('meu_arquivo.txt', 'r', encoding='utf8')  # alias de 'utf_8'
    arquivo = open('meu_arquivo.txt', 'r', encoding='UTF')  # alias de 'utf_8'
    
    arquivo = open('meu_arquivo.txt', 'r', encoding='latin_1')
    arquivo = open('meu_arquivo.txt', 'r', encoding='latin1')  # alias de 'latin_1'
    arquivo = open('meu_arquivo.txt', 'r', encoding='iso-8859-1')  # alias de 'latin_1'
    
    arquivo = open('meu_arquivo.txt', 'r', encoding='ascii')
    arquivo = open('meu_arquivo.txt', 'r', encoding='us-ascii')  # alias de 'ascii'
    arquivo = open('meu_arquivo.txt', 'r', encoding='646')  # alias de 'ascii'
    
        
    30.11.2018 / 13:41