Python Note

Python Note

HashFlare

ubuntu pre installed python2.7 pip, python3, pip3,

best strategy is to use with virtualenv

 

#pip issues:

Python Import Error : Cannot Import Name Main In Ubuntu Linux https://www.youtube.com/watch?v=aFQbFh_HXMA

 

################################################

python 2.7安装过程

step 1: 自然是install python2.7

python-2.7

error:

python version2.7 required not found in the registry

solve: http://effbot.org/zone/python-register.htm

 

step 2: easyinstaller 如果不安装会缺少很多引用

python-ez

step 3: openxml 十分有用

python-openxml

step 4: mysql

error:

C:\data>python ***.py stop
Traceback (most recent call last):
File “***.py”, line 8, in <module>
import MySQLdb
File “C:\Python27\lib\site-packages\MySQLdb\__init__.py”, line 19, in <module>
import _mysql
ImportError: DLL load failed: %1 is not a valid Win32 application.

solve:注意32位和64位的问题 必须和step 1一致 MySQL-python-1.2.4.win32-py2.7.exe  MySQL-python-1.2.4b4.win32-py2.7.exe

基本功能测试

#! /usr/bin/env python
#coding=utf-8
import time
import os
from openpyxl import load_workbook
import sys
import traceback
import MySQLdb

config = {
‘username’:’test’,
‘db’:’test’,
‘password’:’test’
}

def InsertDB(name,title,content):
db = MySQLdb.connect(host=”localhost”, # your host, usually localhost
user=config[‘username’], # your username
passwd=config[‘password’], # your password
db=config[‘db’],
charset = ‘utf8’
)
cur = db.cursor()
result=False
try:
cur.execute(‘call SomeSP(%s,%s,%s)’,(name,title.encode(‘utf8’),content.encode(‘utf8’)))
rows = cur.fetchall()
result=rows[0][0] != 0
print result
except MySQLdb.Error, e:
try:
print “MySQL Error [%d]: %s” % (e.args[0], e.args[1])
except IndexError:
print “MySQL Error: %s” % str(e)
cur.close()
if result:
db.commit()
db.close()
return result
def AnalyzeFile(fileName):
try:
wb = load_workbook(fileName)
ws = wb.active
rowNumber = ws.get_highest_row()
for rx in range(2,rowNumber+1):
name = ws.cell(row=rx,column=1).value
title = ws.cell(row=rx,column = 2).value
content = ws.cell(row=rx,column=3).value
InsertDB(name,title,content)
return True
except :
print ‘Aanalyze file error for ‘ + fileName
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback)
return False

def CheckFile():
currentPath = os.getcwd()
files = os.listdir(currentPath)
if (len(files) == 0):
return

for file in files:
if ‘.xlsx’ in file:
print ‘Analyzing file ::’ + file
if AnalyzeFile(file):
os.remove(file)
print ‘done’
else:
print ‘die’

def Monitor():
while True:
#print time.ctime(time.time())
CheckFile()
time.sleep(10)

Monitor()

 

Compare consecutive columns of a file and return the number of non-matching elements

https://www.biostars.org/p/145352/

CSV new-line character seen in unquoted field error

http://stackoverflow.com/questions/17315635/csv-new-line-character-seen-in-unquoted-field-error

Comparing two CSV files and searching for similar items

http://stackoverflow.com/questions/5268929/python-comparing-two-csv-files-and-searching-for-similar-items

 

Python Web Framework – tornado

友荐云推荐