Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python Toolbox files (with some shell and Perl as well)
import sys
text = sys.argv[1]
print text
#!/usr/bin/python
import sys
file = sys.argv[1]
for entry in open(file):
i = 1
results = entry.split(',')
print results
for result in results:
print i, results[i-1]
i = i + 1
more file.txt | awk '{print "\""$0"\","}' | perl -pi -e 's/\n//g'
import sys
text = sys.argv[1]
print ",".join(["'" + line[:-1].strip() + "'" for line in open(text)])
import csv
reader = csv.reader(open("samples/sample.csv"))
for title, year, director in reader:
print year, title
# http://www.pythonforbeginners.com/systems-programming/using-the-csv-module-in-python/
import csv
ifile = open('test.csv', "rb")
reader = csv.reader(ifile)
ofile = open('ttest.csv', "wb")
writer = csv.writer(ofile, delimiter='\t', quotechar='"', quoting=csv.QUOTE_ALL)
for row in reader:
writer.writerow(row)
ifile.close()
ofile.close()
def mysql_firing(pk):
import MySQLdb
conn = MySQLdb.connect(host="xxx", db="xxx", user="xxx", passwd="xxx")
curs = conn.cursor()
sql_str = """select language_id from comments where pk = %s""" % (pk)
curs.execute(sql_str)
rows = curs.fetchall()
curs.close()
conn.close()
return rows
import MySQLdb
conn = MySQLdb.connect(host="xxx", db="xxx", user="xxx", passwd="xxx")
curs = conn.cursor()
file_to_test = 'test.txt'
for line in open(file_to_test).readlines():
entry = line.split('\t')
comment = entry[1].replace('""', '\"')
sql_str = "INSERT INTO my_table (lang, text, uat) VALUES ('%s', %s, %s)" % (entry[0], unescape(comment), entry[2][:-1])
curs.execute(sql_str)
conn.commit()
break
curs.close()
conn.close()
from datetime import datetime
t = datetime.strptime(end_time, "Run end time: %Y-%m-%d %H:%M:%S.%f\n")
now = datetime.now()
diff = now - t
if diff.seconds > 3600:
print 'hello world'
for x in `ls`; do cd $x; git log > $x-git.log ; echo $x; cd ..; done
def get_reverse_binary_result(n):
return int("{0:b}".format(n)[::-1], 2)
if __name__ == "__main__":
print get_reverse_binary_result(4)
# Given a list of numbers, one number per line, transform the list into a single line, with each number
# surrounded by single quotes and delimited by commas.
more numbers.txt | awk '{print "\""$0"\","}' | sed 's/\"/''/g' | perl -pi -e 's/\n//g'
echo '\n'
print [elem for elem in a if not emails_regex.search(elem)]
import sys
for line in open(sys.argv[1]):
print line[:-1]
from datetime import datetime
final_answer = {}
import sys
for line in open(sys.argv[1]):
v = line[:-1].split()
user = v[2]
d = datetime.strptime(v[3], "[%d/%b/%Y:%H:%M:%S]")
try:
if d > final_answer[user]:
final_answer[user] = d
except:
final_answer[user] = d
for k, v in sorted(final_answer.iteritems()):
print k + "," + str(v)
#!/usr/bin/python
import sys, re
file = sys.argv[1]
for entry in open(file):
i = 1
results = entry.split('|')
for result in results:
print i, result
i = i + 1
perl -pi -e 's/\n/ /g' <file name>
#!/usr/bin/python
# Source: http://www.fredshack.com/docs/python.html
import os, sys, time
import apsw
print "Using APSW file",apsw.__file__
print "APSW version",apsw.apswversion()
print "SQLite version",apsw.sqlitelibversion()
if os.path.exists("dbfile"):
os.remove("dbfile")
connection=apsw.Connection("dbfile")
cursor=connection.cursor()
cursor.execute("begin")
cursor.execute("create table foo(x,y,z)")
cursor.execute("insert into foo values(1,2,3)")
cursor.execute("insert into foo values(4, 'five', 6.0)")
cursor.execute("commit")
for row in cursor.execute("select * from foo"):
print row
for m,n,o in cursor.execute("select x,y,z from foo"):
print m,n,o
connection.close(True)
import time
t = time.strptime(str(t_str), "%Y-%m-%d %H:%M:%S")
secs = time.mktime(t)
return time.strftime("%Y-%m-%d%%20%H:%M:%S", time.gmtime(secs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment