TXT File Manipulation Using Python

0

Let's say I have a arquivo.txt that is separating the information only by the comma, for example,

X,Y,Z,W         
1,2,3,4

How do I modify this file and save it so that a table is left without the presence of these commas (see the following illustration)?

X Y Z W         
1 2 3 4
    
asked by anonymous 07.04.2018 / 23:32

1 answer

0

Come on, the first step is to manipulate the TXT file. To do so, take a look at this link: link

With this, the logic for implementation is to follow the following steps: 1. Read the file lines; 2. Replace the "," characters with "" and store them in a variable; 3. Write the text stored in the variable in the same file.

Implementing Logic:

fileRead = open("arquivo.txt","r")

textoNovoArquivo = ''
for line in fileRead:
    textoNovoArquivo += line.replace(","," ")

fileRead.close()

fileWrite = open("/home/enio/python/arquivo.txt","w")

fileWrite.write(textoNovoArquivo)

fileWrite.close()

I hope I have helped. :)

    
08.04.2018 / 02:11