Posts
 
Reputation
Joined
Last Seen
Ranked #463
Strength to Increase Rep
+8
Strength to Decrease Rep
-2
95% Quality Score
Upvotes Received
26
Posts with Upvotes
23
Upvoting Members
16
Downvotes Received
1
Posts with Downvotes
1
Downvoting Members
1
13 Commented Posts
2 Endorsements
Ranked #621
Ranked #305
~113.34K People Reached
Favorite Forums
Favorite Tags
Member Avatar for RMartins

[QUOTE=RMartins;537916]but how can i prove for example that the result of sqrt(n) is an integer?[/QUOTE] You can do this (maybe is there more elegant way) [CODE] if math.sqrt(n) == float(int(math.sqrt(n))): print "sqrt(n) is an integer" [/CODE]

Member Avatar for pythonBasic
0
719
Member Avatar for gishi

[CODE] import csv reader = csv.reader(open("c:\sample.dat")) d={} for row in reader: d[row[0]]=row[1:] [/CODE]

Member Avatar for Gribouillis
0
15K
Member Avatar for lionaneesh

Just a quick point. To turn an object into string, you can define the special method __repr__ in your object : [CODE] class myobject: def __init__(self, name): self.name=name def __repr__(self): return "my name is %s" % (self.name) o=myobject("toto") print o > toto [/CODE]

Member Avatar for mail2sanjay
1
861
Member Avatar for jcmeyer

I'd write [CODE] self.cursor.execute("INSERT INTO DatabaseName (C1, C2, C3, C4, C5) SELECT (C1, C2, C3, C4, C5) FROM DatabaseName WHERE C1='%s'", [newC1Value]) [/CODE]

Member Avatar for webstart
0
217
Member Avatar for sys73r

[QUOTE=sys73r;1691325]my bad, the code i got is: input.csv 1,2,text, date, qwertyuiopasdfghjklñzxcvbnm, yhnujmik,2121212121 [CODE]import csv reader = csv.reader(open('input.csv', 'rb'), delimiter=',',quoting=csv.QUOTE_NONNUMERIC)) csv_out = csv.writer(open('output.csv', 'w')) for row in reader: content = row[0] + row[1] + row[5] + row[6] csv_out.writerow( content )[/CODE] which is giving me: 1,2,y,h,n,u,j,m,i,k,2,1,2,1,2,1,2,1,2,1 instead of 1,2,yhnujmik,2121212121 thanks![/QUOTE] Ok with …

Member Avatar for sys73r
0
196
Member Avatar for david_wislon

This code goes recursively in all the sudirs of your rootdir. Open all the files, read the lines, calls a function to process your line (here, it does nothing) and overwrite your files with your new lines [code=python] import os rootdir='c:\Your\Path' def doWhatYouWant(line): return line for subdir, dirs, files in …

Member Avatar for paxton91
0
699
Member Avatar for flebber

Another method [CODE] import fnmatch pattern = '*.rtf' files = ['doc.txt', 'stuff.rtf', 'artfunction.exe', 'rtfunc.bat', 'doc2.rtf'] print('\n'.join(filename for filename in fnmatch.filter(files, pattern))) [/CODE]

Member Avatar for TrustyTony
0
335
Member Avatar for rssk

A classical method using list comprehensions [CODE] dict([(list[i], list[i+1]) for i in range(0, len(list), 2)]) [/CODE] function dict takes a list of tuples for arguments and uses the first term of the tuple as key and the second as value If you need more explanations, don't hesitate to ask.

Member Avatar for woooee
0
99
Member Avatar for blacknred

you have to do [code] folder = r'\\192.168.0.12\myshare1' # OR folder = '\\\\192.168.0.12\\myshare1' [/code]

Member Avatar for jice
0
129
Member Avatar for ThePythonNoob

Something like this : [CODE] import random class TV(object): def __init__(self): # self refers to the object itself # Don't mix up an object instance and an object class. # Class is the definition of a type of object (for example TVs all have a brand and channels) # instance …

Member Avatar for lrh9
0
209
Member Avatar for D_rOiD

Here are some function you can look at : - os.listdir(mydir) will list all the files from "mydir" - fnmatch.filter(files_list, "*.txt") will give you a list of all text files in the files_list (tip : fnmatch.filter(os.listdir(mydir)) - os.path.join(mydir, file) will give you the absolute name of the file - os.walk(mydir) …

Member Avatar for jice
0
4K
Member Avatar for tony75

[CODE] import glob print "\n\n".join(["".join([l for l in open(inf).readlines()[:2]]) for inf in glob.glob("C:\\path\\to\\the\\dir\\*.txt")]) [/CODE]

Member Avatar for TrustyTony
0
267
Member Avatar for animanga

Hi, You can use () to make it work (if you really want a one liner)... [CODE] def l(): return (lambda ...)() [/CODE] [CODE] def l(): return (lambda x=input("Enter the text to be decrypted: "),y=int(input("Enter the shift amount: ")): print("The decrypted text is: "+"".join(list(map(lambda x: chr(ord(x)+y),x)))))() [/CODE]

Member Avatar for animanga
0
756
Member Avatar for Zaffron

Can you put the code you use for writing the content of the chest (i suppose : chest.printChest()) and the datas that are written ? BTW, for this kind of thing, you should have a look at __repr__ function.

Member Avatar for woooee
0
454
Member Avatar for xxhellothere

[QUOTE=richieking;1491374][CODE]print(["/".join(date.split("-"))])[/CODE] 2011/03/03 from my htc phone ;)[/QUOTE] I'd rather do [CODE] date.replace('-','/') [/CODE]

Member Avatar for jice
0
158
Member Avatar for dustbunny000

You can try this : [CODE] protein="GWEIQPYVWDECYRVFYEQLNEEHKKIFKGIFDCIRDNSAPNLATLVRVTTNHFTHEQAMMDAVKFSEVLPHKKMHRDFLEKLGGLSAPVDNHIKGTDFKYKGKAKNVDYCKEWLVL" pp="LLCCCCCCCCCCCCCCCCCCHHHHHHHHHHHHHHHHHCHHHHHHHHHHHHHHCCCCHHHHHHHCLLLCCCCCHHHHHHHHHHHHHHHHHHHCCCCCCCCCCCHHHHHHHHHHHHCCL" gor="cccccccccccchhhhhhhhhhhhhhhhhhhhhhhccccccccceeeeecccccchhhhhhhhhhhcccchhhhhhhhhhhhhccccccccccccccccccccccceeceeccceec" aber="CCCCCCCCCCCCHHHHHHHCCHHHCHHHHHHHHHHCCCCHHHHHHHHHHHCCCCCCHHHHHHHCCCCCCCHCCHHHHHHHHHHCCCCCCCCCCCCCCCCCCCCCCCCCHHHHHHHCC" print "".join([str(i / 10) for i in range(len(protein))]) print "".join([str(i) for i in range(len(protein))]) print protein print pp print gor print aber [/CODE]

Member Avatar for jice
0
231
Member Avatar for jice

Hello there, I'm trying to generate a open office text document with tables generated from a database with the lpod library. I'd like to set the width of the tables columns but i can't manage it. Here is my test code so far : [code] import lpod.document import lpod.table doc …

Member Avatar for jice
0
159
Member Avatar for jice

hi, I try to write a app to automatically re-organize my mails (thunderbird). The first thing I try is to re-create my folders tree, once for each year. So i need to read mbox files and rewrite them [CODE=python] import mailbox mbx=mailbox.mbox("./in_mbox") mbx.lock() of=open("out_mbox", "w") for k, m in mbx.iteritems(): …

Member Avatar for jice
0
185
Member Avatar for novice20

This should work (not tested) [CODE] of = open(my_out_filename, 'w') for line in open(my_in_filename,'r'): if 'STRING' in line or 'INTEGER' in line: of.write(line) of.close() [/CODE]

Member Avatar for ptmcg
0
222
Member Avatar for novice20

You can generate csv files... For an easy csv management, there is a csv module [url]http://docs.python.org/library/csv.html[/url]

Member Avatar for jice
0
47
Member Avatar for acrocephalus

Have a look at this : [URL="http://www.devshed.com/c/a/Python/Database-Programming-in-Python-Accessing-MySQL/"]http://www.devshed.com/c/a/Python/Database-Programming-in-Python-Accessing-MySQL/[/URL] Others here [url]http://www.devshed.com/googlecse.html?cx=partner-pub-6422417422167576%3A6n32xsl5si9&cof=FORID%3A11&ie=ISO-8859-1&q=mysql+python&sa.x=28&sa.y=9&sa=Search&siteurl=www.devshed.com%2Fc%2Fb%2FPython%2F#1333[/url]

Member Avatar for acrocephalus
0
122
Member Avatar for lstensland

Your datas aren't in a csv format... For csv, here is a simple example to read and write datas [CODE] import csv # This class is to define another csv format if you need to class excel_french(csv.Dialect): delimiter=';' quotechar='"' doublequote=True skipinitialspace=False lineterminator='\n' quoting=csv.QUOTE_MINIMAL fic='yourfile.csv' outcsvfic='out.csv' csv.register_dialect('excel_french', excel_french) cr=csv.reader(open(fic,"r"), 'excel_french') # …

Member Avatar for lstensland
0
440
Member Avatar for albertkao

don't forget that, in os.walk, if you remove a dir from "dirs", it's subtree won't be explored. So [code] for dirpath, dirs, files in os.walk(path): for d in dirs: if d.startswith('.'): dirs.remove(d) # d's subtree won't be explored for f in files: if not f.startswith('.'): process_file() [/code]

Member Avatar for jice
0
3K
Member Avatar for macca21

A better way to deal with text files is rather with for loop : [code] of=open(myoutfilename, 'w') for line in open(myfilename,'r'): print line of.write(line.replace(' ', '\t')) of.close() ######### OR ############## # not sure the 2 with on the same line work (and i can't test it right now) # this …

Member Avatar for TrustyTony
0
191
Member Avatar for jpl1993
Member Avatar for jpl1993
Member Avatar for dbphydb

wherever you write : [code] something("%s") % one_variable [/code] write [code] something("%s" % one_variable) [/code]

Member Avatar for dbphydb
0
112
Member Avatar for notuserfriendly

Just one thing. For line iterating, i find it is a bad idea to load the whole file in a list (using readlines()) except if you can't process it sequentially. This will consume memory for really nothing interesting... Python allows iterating directly on file lines : [CODE] for line in …

Member Avatar for woooee
0
504
Member Avatar for pankajpriyam

Sorry but i don't understand what you want... Can you post your code ? part of your sql files ? How do you execute the content of your sql files ? What do you want to write in your logfile ? Your question in far too open for me to …

Member Avatar for jice
0
98
Member Avatar for Dan08

Fast method, using sets : [CODE] lines=open(myfile,'r').readlines() uniquelines=set(lines) open(outfile,'w').writelines(uniquelines) [/CODE] which can be done in only one line : [CODE] open(outfile,'w').writelines(set(open(myfile,'r').readlines())) [/CODE]

Member Avatar for TrustyTony
0
4K