Saturday, March 24, 2012

Hello WORLD

HI there

Installing Packages for Python 2.7

Just type
python setup.py install
python must be in your path
Go to DOS
type SET PATH=%PATH%;c:\Python27

must have install keyword or will get error

How to download PIP for Python 3 under Windows 8

distribute_setup.py
Right click and save the above target


Then click the search button for windows.

Type CMD
to open up a shell.

add python to path
SET PATH=%PATH%;c:\Python32
then CD over to where you saved distribute_setup

execute it
python distribute_setup.py

Now download pip, extract it using 7Zip

http://www.pip-installer.org/en/latest/installing.html

type
python get-pip.py

If you don't do these steps in order you might get an error about setuptools module not installed.

Now you need to add the scripts folder of python where easy_install and now pip resides

Back to the command prompt
set PATH=%PATH%;c:\Python32\Scripts

Now you can install all 20000 packages on
http://pypi.python.org/pypi

So in your DOS shell, type pip install [packagename]


EXAMPLE
C:\Users\Omar\Desktop>pip install pyatom
Downloading/unpacking pyatom
  Real name of requirement pyatom is pyatom
  Downloading pyatom-1.2.tar.gz
  Running setup.py egg_info for package pyatom
Installing collected packages: pyatom
  Running setup.py install for pyatom
      File "c:\Python32\Lib\site-packages\pyatom.py", line 61
        return u'<%s type="xhtml"><div xmlns="%s">%s</div></%s>\n' % \
                                                                 ^
    SyntaxError: invalid syntax

Successfully installed pyatom
Cleaning up...
C:\Users\Omar\Desktop>


MODULES is another name for PACKAGES
make sure no script has the same name as your package
so you can't name your script pyatom or you will get an error
Python: Idle: ImportError: cannot import name pyatom

Python read file directory and output all files to a csv list


import os
import re
import csv
os.chdir('c:\\Users\\Omar\\Output')
filesList=os.listdir()
#https://sites.google.com/site/aboutbiblequran/home/samewords
dirFile=open('directoryListing.csv', 'w', newline='')
csvWriter = csv.writer(dirFile)
for filePath in filesList:
        csvWriter.writerow([filePath])
dirFile.close()

Sunday, March 18, 2012

Read Quran Text file Compare it with Bible and Remove Common Words

import re
wordListPattern=re.compile('[a-z]+',re.IGNORECASE)

#------------------------------------------
# quran
#------------------------------------------
quranFile=open("C:\\Users\\Omar\\Desktop\\QuranEnglishUS\\superquran.txt")
quranLines=quranFile.readlines()
quranFile.close()



lineNumber=0
numberOfLine=len(quranLines)
quranWordList=[]
for line in quranLines:
    line=line.lower()
    lineList=wordListPattern.findall(line)
    quranWordList.extend(lineList)
    lineNumber=lineNumber+1
    #print(lineNumber,":",numberOfLine)
    #print(lineNumber, ":" ,lineList)

print('end quran dump')
quranWordSet=set(quranWordList)


#------------------------------------------
# bible
#------------------------------------------
bibleFile=open("C:\\Users\\Omar\\Desktop\\webtxt\\superbible.txt")
bibleLines=bibleFile.readlines()
bibleFile.close()

lineNumber=0
numberOfLine=len(bibleLines)
bibleWordList=[]
for line in bibleLines:
    line=line.lower()
    lineList=wordListPattern.findall(line)
    bibleWordList.extend(lineList)
    lineNumber=lineNumber+1
    #print("Bible ",lineNumber,":",numberOfLine)

print('end bible dump')
bibleWordSet=set(bibleWordList)

#------------------------------------------
# common word list
#------------------------------------------
commonFile=open("C:\\Users\\Omar\\Desktop\\commonword1000.txt")
commonLines=commonFile.readlines()
commonFile.close()

lineNumber=0
numberOfLine=len(commonLines)
commonWordList=[]
for line in commonLines:
    line=line.lower()
    lineList=wordListPattern.findall(line)
    commonWordList.extend(lineList)
    lineNumber=lineNumber+1

print('end common dump')
commonWordSet=set(commonWordList)


intersectionWordSet = quranWordSet.intersection(bibleWordSet)
differenceWordSet = intersectionWordSet.difference(commonWordSet)
sortedWordSet= sorted(differenceWordSet)

#print(sorted(intersectionWordSet))
print('---cleaned----')
#print())
for s in sortedWordSet:
    print (s)

Saturday, March 17, 2012

Compare to CSV files to see if the rows match.

I needed this for an excel sheet in which I sorted one.
I wanted to double check that I didn't mess up anything when sorting.

 
import csv
origfile=open("C:\\Users\Omar\Desktop\pythonOriginal.csv")
dialect=csv.Sniffer().sniff(origfile.read(1024))
origfile.seek(0)
countorig=0
reader=csv.reader(origfile,dialect)
origlist=[]
for row in reader:
        countorig=countorig+1
        origlist.append('|'.join(row))

        
countcomp=0
complist=[]
compfile=open("C:\\Users\Omar\Desktop\pythonCustom.csv")
readercomp=csv.reader(compfile,dialect)
for row in readercomp:
        countcomp=countcomp+1
        complist.append('|'.join(row))

print (countcomp, " ", countorig )

counti=0
for i in origlist:
        counti=counti+1
        countFound=False
        for j in complist:
                if i==j:
                        countFound=True
                        break
        if countFound==False:
            print("Not Found",counti," ",i)
        else:
            print("Found",counti)