Re: [Tutor] adding more text to a file (Vanam)
Below is the code which can be helpful for better insight: Sample: 1 For the below code, you can provide initially the number of entries (say 4) so that for four times name of person and age is prompted to enter.After writing to file we can read back the contents #Writing number of times the name of person to a file #Opening the file log = open('data.txt','w') x = int(raw_input('enter the number of entries')) for y in range(x): data = raw_input('enter the name of the person') age = int(raw_input('enter age of the person')) print >> log, '%s %d' % (data,age) log.close() #Reading the contents of the file with multiple entries log = open('hello.txt','r') for x in log: print x, log.close() Sample:2 Assuming your query, below is the short piece where name and age is always entered till there is no data entered for person and the same content is written back to the file. log = open('data.txt','w') while True: x = raw_input('enter name') if len(x) == 0: break y = int(raw_input('enter age')) print >> log, '%s %d' % (x,y) log.close() > > Hi, > > I am new to python, and i am trying to make a program that makes like a txt > file database. so i have my whole program in a while True loop, it asks for > information about the person and then writes the information about the person > to a txt file, but i need it to be able to add more entries instead of just > one. > > my code is like this: > > while True: > name = raw_input("what is the name ") > age = raw_input("what is the age ") > > out_file = open("persons.txt", "w") > out_file.write("name: ") > out_file.write(name) > out_file.write("\n") > out_file.write("age: ") > out_file.write(age) > out_file.write("\n") > out_file.write("\n") > out_file.close > > now i want to add like say 5 persons to the txt file, but whenever i enter a > new person it just overwrites the previous, im sure its because of the "w" > but how exactly do i need to change my code to add more text instead of > overwriting? > > > > -- Raghavendra Vanam ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Replacing the string in a file
Hi all, I have been trying to write a script where in it has to replace a particular string in a file and write back to the file without intact of the contents I have tried below mentioned steps, but of no avail: 1. Created a file by name data.txt and included some text (For instance, Then, I started searching for a good book on Python. I couldn't find any! I did find some O'Reilly books but they were either too expensive or were more like a reference manual than a guide. So, I settled for the documentation that came with Python. However, it was too brief and small. It did give a good idea about Python but was not complete. I managed with it since I had previous programming experience, but it was unsuitable for newbies. 2.Read the file contents (For this opened the file in read mode -- log = open('data.txt','r') 3.Iterated through the text using for loop for reading the contents in the file -- for x in log: 4.Replaced the word Python with PYTHON as indicated --- x = x.replace('Python','PYTHON') 6.printed the full contents -- It had printed the full contents by replacing the word Python to PYTHON Python string did not get changed to PYTHON for the reasons that i did not open the file in write mode and written the string [Query]: How to write Python string to PYTHON to that file without intact of the full contents I was successful in routing the contents (with changed string to PYTHON) to a different file. And i am aware that opening the file in write mode will wipe out the previous contents. I have tried appending the content which it does but that is not my goal. 7. Closed the files outside for loop -- log.close() -- Raghavendra Vanam ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Replacing the string in a file
Thanks for your mail. As you have suggested i have changed the mode to 'rw' but it is throwing up an error as below *** IOError: [Errno 22] invalid mode ('rw') or filename: 'data.txt' *** I am using python 2.6.4. But Script is managed to pass with 'a+' mode/r+ mode. log = open('data.txt','r+/a+') for x in log: x = x.replace('Python','PYTHON') print x, log.close() It had properly written and replaced Python to PYTHON. Thanks for your suggestion. On Fri, Jan 22, 2010 at 12:42 PM, Shashwat Anand wrote: > You can try it by reading all data as string, replacing the string and then > write it to target file via opeing it in +w mode. > > f = open('data.txt', 'rw').read().replace('Python', 'PYTHON') > f1 = open('data.txt', > 'w') > f1.write(f) > > HTH > > > On Fri, Jan 22, 2010 at 12:14 PM, vanam > wrote: >> >> Hi all, >> >> I have been trying to write a script where in it has to replace a >> particular string in a file and write back to the file without intact >> of the contents >> >> I have tried below mentioned steps, but of no avail: >> >> 1. Created a file by name data.txt and included some text (For >> instance, >> >> >> Then, I started searching for a good book on Python. I couldn't find >> any! I did find some O'Reilly books >> but they were either too expensive or were more like a reference >> manual than a guide. So, I settled for >> the documentation that came with Python. However, it was too brief and >> small. It did give a good idea >> about Python but was not complete. I managed with it since I had >> previous programming experience, but >> it was unsuitable for newbies. >> >> >> 2.Read the file contents (For this opened the file in read mode -- log >> = open('data.txt','r') >> >> 3.Iterated through the text using for loop for reading the contents in >> the file -- for x in log: >> >> 4.Replaced the word Python with PYTHON as indicated --- x = >> x.replace('Python','PYTHON') >> >> 6.printed the full contents -- It had printed the full contents by >> replacing the word Python to PYTHON >> >> Python string did not get changed to PYTHON for the reasons that i did >> not open the file in write mode and written the string >> >> [Query]: How to write Python string to PYTHON to that file without >> intact of the full contents >> >> I was successful in routing the contents (with changed string to >> PYTHON) to a different file. >> >> And i am aware that opening the file in write mode will wipe out the >> previous contents. >> >> I have tried appending the content which it does but that is not my goal. >> >> >> 7. Closed the files outside for loop -- log.close() >> -- >> Raghavendra Vanam >> ___ >> Tutor maillist - tu...@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > -- Raghavendra Vanam ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] [File Input Module]Replacing string in a file
Hi all, As it was suggested before in the mailing list about the query regarding replacing string in the file, i have used the module File input for replacing the string in the file. For understanding and execution purpose, i have just included Python as a string in the file and want it to be replaced to PYTHON. Below are my queries and code: (Correct me if my understanding is wrong???) 1)) import fileinput x = fileinput.input('data.txt',inplace=0) for line in x: line = line.replace('Python','PYTHON) print line, x.close() The above piece of code will not create any backup file but it will replace PYTHON (Print on the console) but not physically write to the file. 2))) import fileinput x = fileinput.input('data.txt',inplace=1) for line in x: line = line.replace('Python','PYTHON') print line, x.close() The above piece of code will create backup file but hidden (in the form of bak file) and it will physically write to the file -- I have verified the contents of data.txt after the file operation and it had written successfully.But why it is not printing line i.e. string in the file on the console. 3))) import fileinput x = fileinput.input('data.txt',inplace=1) for line in x: line = line.replace('Python','PYTHON') x.close() The above piece of code after execution is wiping out the full contents. But addition of print line, is exactly replacing the string, what exactly addition of print is making difference??? 4))) import fileinput x = fileinput.input('data.txt',inplace=1,backup='content.txt') for line in x: line = line.replace('Python','PYTHON') print line, x.close() The above piece is creating a backup file by name data.txtcontent.txt (I am not sure whether created file name is correct or not?) and to the back up file it had added previous content i.e., Python and it had replaced the contents in data.txt to PYTHON 5))) Suppose if data.txt has string Python written in Font size 72 and when i display the string on the console ie. by below piece of code import fileinput x = fileinput.input('data.txt',inplace=0) for line in x: print line, x.close() It wouldnt print with the same Font size on the console (This wont prove anything wrong as the same font could be backed with a different file name) Do let me know if my understanding is correct. -- Raghavendra Vanam ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] [Installing Spyder IDE]:Help
I have been trying to install spyder ide but of no avail. Downloaded and installed spyder-1.03_py26.exe I have downloaded pyqt4 for that purpose(Direct Installer) pyqt-py2.6-gpl-4.7-1.exe after installing the above softwares and then launching Spyder it is showing initialising and gets disappears. Is that something or dependency i have missed to install? -- Raghavendra Vanam -- Raghavendra Vanam ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Inputting elements of a list
For standard input from the keyboard raw_input will be used for string and input for number.Suppose i want to input a list of some elements how could it be done. Actually i am looking for sorting out elements in a list.Belowis the given script list = [45,4,5] list.sort() for y in list: print y ->this would sort out elements in that list. my question is in the above script all the elements that list carry are defined i want to input some elements in the list how could it be done -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Difference between filter and map
i want to know the difference between filter(function,sequence) and map(function,sequence).I tried for a simple script with an function which finds the square of the number,after including separately filter and map in the script i am getting the same results for instance def squ(x): return x*x filter(squ,range(1,3))->1,4(output) map(squ,range(1,3)->1,4(output) -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
ya i am sure about that i am using python editor which has python intrepreter attached to it i got the same output for both filter and map def squ(n): y = n*n print y filter(y,range(3))->0 1 4 map(y,range(3))->0 1 4 On 1/23/07, Kent Johnson <[EMAIL PROTECTED]> wrote: vanam wrote: > i want to know the difference between filter(function,sequence) and > map(function,sequence).I tried for a simple script with an function > which finds the square of the number,after including separately filter > and map in the script i am getting the same results for instance > def squ(x): > return x*x > filter(squ,range(1,3))->1,4(output) > map(squ,range(1,3)->1,4(output) Are you sure about that? I get In [1]: def sq(x): return x*x ...: In [2]: filter(sq, range(3)) Out[2]: [1, 2] In [3]: map(sq, range(3)) Out[3]: [0, 1, 4] map(fn, lst) returns a new list with fn applied to each element of lst. In terms of list comprehensions, it is [ fn(x) for x in lst ]. filter(fn, lst) returns a new list containing all elements of the original list for which fn(x) is true. As a list comprehension, it is [ x for x in lst if fn(x) ] Kent -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
Yes i did a mistake in expressing my problem below are the instances of the script and its corresponding output,for each instance its giving contrasting result i want explanation for that [1]:def squ(n): return n*n filter(squ,range(3))>output is not seen on the interpreter map(squ,range(3))->output not seen on the interpreter print filter(squ,range(3))->output is [1,2] print map(squ,range(3))-->output is [0,1,4] [2]:def squ(n): y = n*n print y filter(squ,range(3))-->Below is the output 0 1 4 map(squ,range(3))-->Below is the output 0 1 4 print filter(squ,range(3))--->Below is the output 0 1 4 [] print map(squ,range(3))-->Below is the output 0 1 4 [None,None,None] I want to know why in each case its giving different results and diff between filter and map On 1/23/07, Kent Johnson <[EMAIL PROTECTED]> wrote: vanam wrote: > ya i am sure about that i am using python editor which has python > intrepreter attached to it i got the same output for both filter and map > def squ(n): >y = n*n > print y > filter(y,range(3))->0 1 4 > map(y,range(3))->0 1 4 This is quite different that what you posted the first time. This function squ() *prints* n*n but *returns* None (since it has no explicit return statement). The previous squ() actually returned n*n. But the results are still different if you look carefully: In [2]: def sq(n): ...: y=n*n ...: print y ...: ...: In [3]: map(sq, range(3)) 0 1 4 Out[3]: [None, None, None] The function sq() is called for each element of range(3) and prints the square. This is why 0, 1, 4 are printed. But the value returned from map() is the list [None, None, None] which is the accumulated return values from calling sq(). In [4]: filter(sq, range(3)) 0 1 4 Out[4]: [] Here, sq() is still called for each element of range(3). Since the printing is from sq(), 0, 1 and 4 are still printed. But the return value is an empty list [] because None is not true so sq(n) is not true for any elements of range(3). Kent -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python Branches
i started slowly learning python as i dont have much exposure to it and i wanted to know what are all branches there in python actually i installed py 2.5 recently i come across question in groups about the gui where in pygtk should be installed.My question is without this installation could i able to develop any application using py2.5?One more thing is what is tkinter?Can any one explain what are all branches in python and how it could be used? -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Diff between opening files in 'r' and 'r+' mode
i want to know the difference between 'r' mode and 'r+' mode 1.i = open('c:\python25\integer.txt','w')>for writiing i.write('hai')->written some content in text file i = open('c:\python25\integer.txt','r')>for reading print i.read()>for printing the contents in that text file i = open('c:\python25\integer.txt','w')-->for writing i.write('how')---?Rewrite the contents print i.read() [MY QUESTION]:i want to read the text file contents cant it be done by giving (print i.read())? Before going to next question [I deleted all the contents in the text file] 2.i = open('c:\python25\integer.txt','r+')-For reading and writing i.write('hai')->written some content to text file print i.read()->{؆('c:\python25\integer.txt','w') i write('') print i.read()how') i = open('c:\python25\integer.txt','r') print i.read() i = open('c:\python25\integer.txt','w') i.write() i = open('c:\python25\integer.txt','r') print i.read() } --->Thats what i saw on interpreter(In curly braces) when i ran the script [MY QUESTION]:1.from where the above in curly braces is printed?and i have written only 'hai' to the text file 2.Should i recall again the opening of the file in 'r' mode to read the file? -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Explanation of Pickle
can any one explain about pickle i read in the book but they have not provided any example for that so please explain with a simple example -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Recursive function
hi all, i am new to programming, i just started writing scripts in python about functions.There is a program by name hangman where in i take input three characters and then concatenate the resultant output is compared to a three letter string, if it is similar it will display the word as correct if not i want to execute the same function which evokes to prompt for three characters as input. The problem is i am not getting a runtime error. below is the piece of code: #word Hangman print "Welcome to the Hangman" print print a = raw_input("enter 1st letter=") b = raw_input("enter 2nd letter=") c = raw_input("enter 3rd letter=") def cmp(): d = (a+b+c); if (d=='PAN'): print "word" 'd' "is correct" else: print "Try Again" CALL(); def CALL(): cmp(); cmp() -- Raghavendra Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Usage of for loop
Hi all, i am beginner to this python language and slowing learning the language by referring docs.I am trying to understand the for loop i.e., usage of for loop in python,unlike c where i can give condition in python it is simple iterating over sequence. I am trying tounderstand the below lines of code but of no avail. a = ["cat", "window","defenestrate"] for x in a: print x, len(x) i cant understand what x is doing here and what is the purpose of it can anyone help me out here? -- Raghavendra Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor