Place the information contained in Linha2[0:6] in a ". txt file

Asked

Viewed 65 times

-3

Place in a file ". txt" the information contained in Linha2[0:6] which is:

['Mem ria', 'f¡sica', 'total:', '7.988', 'MB']
['Memria', 'f¡sica', 'available', '5.277', 'MB'']
['Mem ria', 'Virtual:', 'Size', ’M', 'ximo:', '9.268']
['Mem ria', 'Virtual:', 'Available:', '6.094', 'MB']
['Mem ria', 'Virtual:', 'Em', 'Use:', '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])

EXIT:

Host name: FPAL169325
Operating system name: Microsoft Windows 8.1 Pro
Operating system version: 6.3.9600 N/A compiles‡Æo 9600
Operating system manufacturer: Microsoft Corporation
Configures‡æo of the OS: This‡æo member work
Compile type‡Æo of the operating system: Multiprocessor Free
Registered owner: SENAC FACULTY PORTO ALEGRE
Organizes‡Æo registered: SENACRS
Identifies‡Æo of the product: 00261-80511-41292-A269
Date of installation‡Æo original: 08/01/2018, 19:29:13
System Boot Time‡æo: 26/10/2018, 20:47:23
System manufacturer: LENOVO
System model: 32092F2
System type: x64-based PC
Processor(s): 1 processor(s) installed(s). [01]: Intel64 Family 6 Model 58
Stepping 9 Genuineintel ~3201 Mhz
BIOS Versæo: LENOVO 9SKT69AUS, 17/05/2013
Windows folder: C: windows
System folder: C: windows system32
Initialize device: Device Harddiskvolume1
System location: en;Portuguˆs (Brazil)
City of entry: en;Portuguˆs (Brazil)
Zone Hor river: (UTC-03:00) Brazil
Memria f¡sica total: 7.988 MB
Available memory: 5.277 MB
Virtual Memory: Size M ximo: 9.268 MB
Virtual Memory: Available: 6.094 MB
Virtual Memory: In Use: 3.174 MB
Page file location(s) ‡Æo: C: pagefile.sys
Dom¡nio: fspoaeduc.com.br
Login Server: CROM
Hotfix(s): 165 Hotfix(s) installed(s). [01]: Kb2899189_microsoft-Windows-Cameracodec-Package

  • What is the expected result? It’s not giving to understand what you need.

  • Read each list as a line ex.: Memria total 7.988 MB

  • But don’t you have to put it in a file? What’s inside sai_systeminfo.txt? You could edit the question with this information?

  • Yes do what was shown and put in a txt

  • What you added is output or is the file saida_systeminfo.txt??

  • This is the content of saida_systeminfo.txt

  • From what I understand you want to read this file and create another file with the memory data copied to this new file. That’s it?

  • Perfeito Fernando

  • I’ll formulate an answer then. But you need to try to be clearer when asking a question, you might already have a good answer if it were easier to understand.

  • Thanks for the help

  • I don’t really know the question editing tools, so I try to pollute the screen as little as possible

  • Homer, in help center has tips on how to edit/format your questions, and on FAQ also has a lot of information about it. Anyway, even if you don’t fully master the editing tools, first worry about making the question clear (see tips on [Ask] and how to mount a [mcve]). A clear and poorly formatted question can be edited by other users, already a question well formatted but not very clear does not have much to do, except ask the author clarify...

  • Okay, thank you hkotsubo

Show 8 more comments

1 answer

3


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

  1. Open a file for reading (open('meu_arquivo', 'r'))
  2. Open a file for recording (open('outro_arquivo', 'w'))
  3. Read the file line by line for linha in arquivo:
  4. Test if the line being read should be copied (in this case I used the str.startswith())
  5. Write the line in the output file (View file-Objects methods).

I advise you to open files with the with, this way it takes care of closing the file at the end of the reading or in case of an error occurs.

Below an example running:

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 code working


Edit

Since the contents of the file that AP posted contains some errors of encoding, I’ll just point out that the function open() has an optional parameter encoding where you can use one of encodings python-supported. 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'
  • It’s even been formatted. And you didn’t even need to list the information

  • Thank you very much Fernando!!!!

  • The important thing is to understand how it works. Good luck

  • @Homer I edited the answer with one more piece of information that might help your case.

  • Thank you for the content

Browser other questions tagged

You are not signed in. Login or sign up in order to post.