In terms of files external files to python, such as dat, text, cvs… files, the command that you use to open them is……………….. OPEN!!!!

open(“filename.extension”,”r or w”)

You put r if you are going to read the file

You put w if you want to write the file

There are others parameter you can type there, but those are the basics

So let’s start, I am going to use the 93car.dat.tx the Journal of Statistics Education gives us.

What I want to do, is to delete all those strange number to only left the brand of the car and rthe model

See the original file:

original fiel

And how I want it to end:

desired file

So let’s start

The first thing to do is to open the file in reading mode (that means r at the end of the parenthesis)  and to store aaaaalll the data inside a variable inside python, I called it text, after that close it.

open in read

Then I will open the file in writing mode to start modifying it, so first, I have to get rid of the even lines.

open in write

One way te start reading and changing the lines is with a for loop

for (any variable, I chose line) in (where you stored all the data, in my case is text)

for line in text:

That is the main loop, and since I want to get rid of the even lines, I am going to write the lines only if is even BEACUSE we count the first iteration as one, see that x?

after doing that, close it!

open your file, and you are going to see….. YES!

middle.PNG

You have just modified your first file from python!

Now, I don like all those number, so I want to errase them, I know all these numbers start on the position number 30 en every line, so starting over and doing the same process EXCEPT what is inside the for.

start over

You see, what we are going to do inside the for loop is to write each line, but cutted; you see that [0:29]? that means that it is only going to write from the first space to the 29th of each line

just cmodesadas

And now open your file…. and it has to look like this, you did it!

 

asdfas

As you can see, it is very easy, you just write up what you want

 

Github link

 

b1e0b19965f87e7b83b66070e394bb33

CC BY 4.0 Mastery 30: Files by charliegdrummer is licensed under a Creative Commons Attribution 4.0 International License.