In terms of files external files to python, such as dat, text, cvs… files, the command that you use to open them is……………….. OPEN!!!!<\/p>\n
open(“filename.extension”,”r or w”)<\/p>\n
You put r if you are going to read the file<\/p>\n
You put w if you want to write the file<\/p>\n
There are others parameter you can type there, but those are the basics<\/p>\n
So let’s start, I am going to use the 93car.dat.tx the Journal of Statistics Education gives us.<\/p>\n
What I want to do, is to delete all those strange number to only left the brand of the car and rthe model<\/p>\n
See the original file:<\/p>\n
<\/p>\n
And how I want it to end:<\/p>\n
<\/p>\n
So let’s start<\/p>\n
The first thing to do is to open the file in reading mode (that means r at the end of the parenthesis) \u00a0and to store aaaaalll the data inside a variable inside python, I called it text, after that close it.<\/p>\n
<\/p>\n
Then I will open the file in writing mode to start modifying it, so first, I have to get rid of the even lines.<\/p>\n
<\/p>\n
One way te start reading and changing the lines is with a for loop<\/p>\n
for (any variable, I chose line) in (where you stored all the data, in my case is text)<\/p>\n
for line in text:<\/p>\n
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?<\/p>\n<\/p>\n
after doing that, close it!<\/p>\n
open your file, and you are going to see….. YES!<\/p>\n
<\/p>\n
You have just modified your first file from python!<\/p>\n
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.<\/p>\n
<\/p>\n
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<\/p>\n
<\/p>\n
And now open your file…. and it has to look like this, you did it!<\/p>\n
<\/p>\n
<\/p>\n
As you can see, it is very easy, you just write up what you want<\/p>\n
<\/p>\n
Github link<\/a><\/span><\/p>\n <\/p>\n <\/p>\n <\/a> <\/p>\n","protected":false},"excerpt":{"rendered":" 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 … Continúa leyendo Mastery 30: Files<\/span><\/a><\/p>\n","protected":false},"author":191,"featured_media":28419,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,3],"tags":[431,45,39],"_links":{"self":[{"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/posts\/28430"}],"collection":[{"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/users\/191"}],"replies":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/comments?post=28430"}],"version-history":[{"count":1,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/posts\/28430\/revisions"}],"predecessor-version":[{"id":28431,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/posts\/28430\/revisions\/28431"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/media\/28419"}],"wp:attachment":[{"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/media?parent=28430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/categories?post=28430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kenscourses.com\/tc101fall2015\/wp-json\/wp\/v2\/tags?post=28430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}