Re: [Tutor] Replacing the string in a file
> > vanam, 22.01.2010 07:44: > > > [Query]: How to write Python string to PYTHON to that file without > > intact of the full contents > > What one would normally do is: read the file (line by line if that's > ok in > your case), replace the word by the new one, write the new line to a > new > temporary file. When done, move the new file over the old one. That > also > makes sure that your changes are 'atomic' (as good as it gets), i.e.. > your > original file is still there when you encounter an error during the > replacement run. > > Stefan heys, I have used the fileinput module for this quiet alot. It has an inplace option which does what you want and a backup option that keeps a backup. http://docs.python.org/library/fileinput.html http://www.doughellmann.com/PyMOTW/fileinput/index.html stefan (another stefan) ___ 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
Re: [Tutor] Replacing the string in a file
@vanam: If you open the file again with only '+w' and not '+rw', I guess you'll not be getting this error - > IOError: [Errno 22] invalid mode ('rw') or filename: 'data.txt' On Fri, Jan 22, 2010 at 1:51 PM, vanam wrote: > 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 - Tutor@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
Re: [Tutor] Replacing the string in a file
Stefan Lesicnik, 22.01.2010 09:11: > I have used the fileinput module for this quiet alot. It has an inplace > option which does what you want and a backup option that keeps a backup. > > http://docs.python.org/library/fileinput.html > http://www.doughellmann.com/PyMOTW/fileinput/index.html Ah, right, I almost forgot about this one: http://xkcd.com/413/ Stefan ___ 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
and there is the everlasting : http://xkcd.com/353/ :D On Fri, Jan 22, 2010 at 2:16 PM, Stefan Behnel wrote: > Stefan Lesicnik, 22.01.2010 09:11: > > I have used the fileinput module for this quiet alot. It has an inplace > > option which does what you want and a backup option that keeps a backup. > > > > http://docs.python.org/library/fileinput.html > > http://www.doughellmann.com/PyMOTW/fileinput/index.html > > Ah, right, I almost forgot about this one: > > http://xkcd.com/413/ > > Stefan > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ 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
vanam wrote: 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. That won't work. Better test it some more. Without some form of write() call, you're not changing the file. There are several workable suggestions in this thread, and I think fileinput is the easiest one. DaveA ___ 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
The modes for updating files are + suffixed. r+ Open for reading and writing. The stream is positioned at the beginning of the file. w+ Open for reading and writing. The file is created if it does not exist, otherwise it is trun‐ cated. The stream is positioned at the beginning of the file. a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file. The bigger issue here is, that depending upon your filesystem it might be faster to just write a new file with the changed content. If you want to replace it inplace, I'd consider something like that: import mmap f = open("/tmp/test.txt", "r+") m = mmap.mmap(f.fileno(), 0) idx = 0 count = 0 while True: idx = m.find("file", idx) if idx >= 0: m[idx:idx + 4] = "FILE" count += 1 idx += 1 else: print "%d file => FILE replaces" % count break m.close() f.close() andr...@andidesk:/tmp$ python ./test.py 39 file => FILE replaces You've got basically 3 strategies: 1) read the file completely into memory via read => uses memory. 2) read the file block-wise => complicated because you need to recognize matches that are split between blocks. 3) memory map the file => uses address space, but the OS does know that you are using that "memory" to access the file. 1 is the shortest code, most resource usage. 2 is complicated code, low resource usage, 3 is slightly longer (because mmap objects have no .replace method), and comparable resource usage to 2. Andreas Andreas Am Freitag, 22. Januar 2010 12:23:53 schrieb Dave Angel: > vanam wrote: > > 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. > > > > > > > > That won't work. Better test it some more. Without some form of > write() call, you're not changing the file. > > > There are several workable suggestions in this thread, and I think > fileinput is the easiest one. > > DaveA > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] parse text file
Hello, I have the following http://paste.lisp.org/display/93732 txt file. >From this I would like to extract ... 'ACP' + 'En savoir plus' ); ... map.addOverlay(marqueur[1]);var latlng = new GLatLng(9.696333, 122.985992); so that i get a CSV file: "ACP", "acp.html" , "9.69633", "122.985992" This is what I have so far: >>> file=open('google_map_code.txt', 'r') >>> data = repr( file.read().decode('utf-8') ) >>> from BeautifulSoup import BeautifulStoneSoup >>> soup = BeautifulStoneSoup(data) >>> strongs = soup.findAll('strong') >>> strongs [ALTER TRADE CORPORATION, ANAPQUI, APICOOP / VALVIDIA, APIKRI, ... >>> path = soup.findAll('a') >>> path [En savoir plus, En savoir plus, ... but my problem comes when i try to list the GLatLng: GLatLng(9.696333, 122.985992); >>> StartingWithGLatLng = soup.findAll(re.compile('GLatLng')) >>> StartingWithGLatLng [] Thanks Norman ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create an object from a class in dll with ctypes?
hi, it worked great, i am just wondering if the resulting .pyd is plattform dependent? If i create one on windows would linux user need another one? Thank you, katrin Original-Nachricht > Datum: Tue, 19 Jan 2010 23:49:14 -0800 > Von: "Mark Tolonen" > An: tutor@python.org > Betreff: Re: [Tutor] create an object from a class in dll with ctypes? > > "katrin schmid" wrote in message > news:0edda7dddff84d639352526b72dbf...@katissspc... > > hi, > > i am getting started with ctypes in python 2.5 and was wondering if > > i would be able to create an object from the class in my dll somehow. > > I only found examples that show how to access a function but the > > function i want to call is part of a cpp class in my dll... > > ctypes is for C DLLs. You might want to look at www.swig.org instead. > > -Mark > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Haiti-Nothilfe! Helfen Sie per SMS: Sende UIHAITI an die Nummer 81190. Von 5 Euro je SMS (zzgl. SMS-Gebühr) gehen 4,83 Euro an UNICEF. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] parse text file
Hi On Fri, Jan 22, 2010 at 7:44 PM, spir wrote: > On Fri, 22 Jan 2010 14:11:42 +0100 > Norman Khine wrote: > >> but my problem comes when i try to list the GLatLng: >> >> GLatLng(9.696333, 122.985992); >> >> >>> StartingWithGLatLng = soup.findAll(re.compile('GLatLng')) >> >>> StartingWithGLatLng >> [] > > Don't about soup's findall. But the regex pattern string should rather be > something like (untested): > r"""GLatLng\(\(d+\.\d*)\, (d+\.\d*)\) """ > capturing both integers. > > Denis > > PS: finally tested: > > import re > s = "GLatLng(9.696333, 122.985992)" > p = re.compile(r"""GLatLng\((\d+\.\d*)\, (\d+\.\d*)\)""") > r = p.match(s) > print r.group() # --> GLatLng(9.696333, 122.985992) > print r.groups() # --> ('9.696333', '122.985992') > > s = "xGLatLng(1.1, 11.22)xxxGLatLng(111.111, .)x" > r = p.findall(s) > print r # --> [('1.1', '11.22'), ('111.111', > '.')] Thanks for the help, but I can't seem to get the RegEx to work correctly. Here is my input and output: http://paste.lisp.org/+20BO/1 > > > la vita e estrany > > http://spir.wikidot.com/ > -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create an object from a class in dll with ctypes?
"katrin schmid" wrote if the resulting .pyd is plattform dependent? If i create one on windows would linux user need another one? A Windows DLL will not work on Linux and a Linux library will not work on Windows. You need one per OS. Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create an object from a class in dll with ctypes?
hi, so how about 32 and 64 bit, do they need separat versions, too? So a pyd is an "actual" dll? Regrads, katrin Original-Nachricht > Datum: Sat, 23 Jan 2010 00:13:53 - > Von: "Alan Gauld" > An: tutor@python.org > Betreff: Re: [Tutor] create an object from a class in dll with ctypes? > > "katrin schmid" wrote > > if the resulting .pyd is plattform dependent? > > If i create one on windows would linux user need another one? > > A Windows DLL will not work on Linux and a Linux library > will not work on Windows. You need one per OS. > > Alan G. > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Haiti-Nothilfe! Helfen Sie per SMS: Sende UIHAITI an die Nummer 81190. Von 5 Euro je SMS (zzgl. SMS-Gebühr) gehen 4,83 Euro an UNICEF. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create an object from a class in dll with ctypes?
"katrin schmid" wrote in message news:20100123021522.155...@gmx.net... hi, so how about 32 and 64 bit, do they need separat versions, too? So a pyd is an "actual" dll? Regrads, katrin Yes, to both questions. -Mark ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create an object from a class in dll with ctypes?
hi, and there is no portable way of distributing it? I could tranlate the cpp code to python but its really lame Regards, katrin Original-Nachricht > Datum: Fri, 22 Jan 2010 20:42:17 -0800 > Von: "Mark Tolonen" > An: tutor@python.org > Betreff: Re: [Tutor] create an object from a class in dll with ctypes? > > "katrin schmid" wrote in message > news:20100123021522.155...@gmx.net... > > hi, > > so how about 32 and 64 bit, > > do they need separat versions, too? > > So a pyd is an "actual" dll? > > Regrads, > > katrin > > Yes, to both questions. > > -Mark > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Haiti-Nothilfe! Helfen Sie per SMS: Sende UIHAITI an die Nummer 81190. Von 5 Euro je SMS (zzgl. SMS-Gebühr) gehen 4,83 Euro an UNICEF. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create an object from a class in dll with ctypes?
hi, so could i build a 64-bit windows version on win 32? And how? I have just been asked ... Regards, katrin Original-Nachricht > Datum: Sat, 23 Jan 2010 00:13:53 - > Von: "Alan Gauld" > An: tutor@python.org > Betreff: Re: [Tutor] create an object from a class in dll with ctypes? > > "katrin schmid" wrote > > if the resulting .pyd is plattform dependent? > > If i create one on windows would linux user need another one? > > A Windows DLL will not work on Linux and a Linux library > will not work on Windows. You need one per OS. > > Alan G. > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Haiti-Nothilfe! Helfen Sie per SMS: Sende UIHAITI an die Nummer 81190. Von 5 Euro je SMS (zzgl. SMS-Gebühr) gehen 4,83 Euro an UNICEF. -- Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 - sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create an object from a class in dll with ctypes?
"katrin schmid" wrote and there is no portable way of distributing it? I could tranlate the cpp code to python but its really lame Native code is native code I'm afraid. You have to rebuild it for every platform. Thats one reason interpreted languages are becoming more popular because they are portable. The downside of portability is loss of speed. Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor