snippsat 661 Master Poster

Cant find what you search in respData.
Do post also post your import.

import urllib.request, urllib.parse

This is the adress,you get data from.

>>> resp.geturl()
'https://ca.finance.yahoo.com/lookup?s=basics'

Do you find Price Book or class="yfnc_tabledata1 in url or in return respData?

Some notes this use JavaScript heavy,and are not a easy site to start with.
Which mean that you may have to use other method than urllib to read site.
I use Selenium to read sites like this.
Then i get executed JavaSript to,and can parse with Beautiful Soup or lxml.

Regex to parse HTML can be a bad choice,
it can work in some cases,but use a parser(BeautifulSoup) is the first choice.
I usually post this link,why not to use regex.

Gribouillis commented: good tips +14
snippsat 661 Master Poster

Pillow fork dos more than just bugfix of orginal PIL.
It now comes with new stuff and serval improvements.
So it can be smart to get Pillow instead of PIL.

snippsat 661 Master Poster

You can not open 2 exe in binary mode,and try to merge these into 1 file.
exe are standalone programs,that has to be open separately.

Here a demo of what i mean,with subprocess and threads to open 2 files.

#2_exe_start.py
import subprocess
import threading

def file_1():
    subprocess.call([r'C:\Windows\notepad.exe'])

def file_2():
    subprocess.call([r'C:\Windows\explorer.exe'])

f1 = threading.Thread(target=file_1)
f2 = threading.Thread(target=file_2)
f1.start()
f2.start()

So this open both notepad and explorer at once.
Then i can make a exe,to open these 2 files.
So here a make 2_exe_start.exe,that's open notepad and explorer.

#make_exe.py
from distutils.core import setup
import py2exe
import sys

def py2_exe(file_in, ico):
    dest_ico = ico.split('.')[0]
    if len(sys.argv) == 1:
        sys.argv.append('py2exe')

    # Py2exe finds most module,here you can include,exclude moduls
    includes = []
    excludes = []
    packages = []
    dll_excludes = []

    # Bundle_files : 3 most stable | bundle_files : 1 create 1 big exe
    setup(options =\
    {'py2exe': {'compressed': 1,
                'optimize': 2,
                'ascii': 0,
                'bundle_files': 1,
                'includes': includes,
                'excludes': excludes,
                'packages': packages,
                'dll_excludes': dll_excludes,}},
                 zipfile = None,

    # Can use console(Command line) or windows(GUI)
    console = [{
              'script': file_in,
              #--| Uncomment for ico
              #'icon_resources' : [(1, ico)],
              #'dest_base' : dest_ico
               }])

if __name__ == '__main__':
    #--| The .py file you want to make exe of
    file_in = '2_exe_start.py'
    #--| Ico in same folder as .py
    ico = 'your.ico'
    py2_exe(file_in, ico)
snippsat 661 Master Poster

To fix your orginal code.

def translate():
    a = {"merry":"god", "christmas":"jul", "and":"och", "happy":"gott", "new":"nytt", "year":"år"}
    return a

for k, v in translate().iteritems():
    print k, v

You need to practice more on using functions.
It make no sense to take a argument a,when all you have in function is a dicionary.
a as mention is not a meaningful variable name.

Something like this for translate.

def my_dict(user_input):
    return {
    "merry": "god",
    "christmas": "jul",
    "and": "och",
    "happy": "gott",
    "new": "nytt",
    "year": "år"
     }.get(user_input, "Not in dicionary")

def translate(word):
    return my_dict(word)

Test:

>>> translate('christmas')
'jul'
>>> translate('hello')
'Not in dicionary'
>>> translate('happy')
'gott'
>>> print '{} {} til alle i Sverige'.format(translate('merry'), translate('christmas'))
god jul til alle i Sverige
snippsat 661 Master Poster

Is this all of your code?
There are missing several function definition here.
You never use return without a function.

Post all of you code and full Traceback.
Or at least code we can run that give this Traceback.
Traceback always give you linenumber where error occur.

Here is a explaintion of UnboundLocalError

snippsat 661 Master Poster

With enumerate().

>>> mylist1 = [1,2,3]
>>> mylist2 = ['a','b','c']
>>> for index, item in enumerate(mylist2, 2):
...     mylist1.insert(index, item)
...     
>>> mylist1
[1, 2, 'a', 'b', 'c', 3]

Flatten list approach.

def flatten(container):
    for i in container:
        if isinstance(i, list):
            for j in flatten(i):
                yield j
        else:
            yield i

def my_code(list1, list2, index, flatten):
    list1.insert(2,index)
    return list(flatten(list1))

if __name__ == '__main__':
    list1 = [1, 2, 3]
    list2 = [2]
    index = ['a','b','c']
    print my_code(list1, list2, index, flatten)
    #--> [1, 2, 'a', 'b', 'c', 3]
snippsat 661 Master Poster

A distribution such as Anaconda make it easy to run iPython notebook without thinking of dependencies.

I aslo have Miniconda wish i run iPython notebook/Spyder for python 3.4 from.
Anaconda is closed enviroment so it would not affect orginal installed Python.
My main version that i use most is still Python 2.7
More about it here

snippsat 661 Master Poster

A little look at concurrent.futures
concurrent.futures has a minimalistic API for threading and multiprocessing.
Only change one word to switch ThreadPoolExecutor(threading) and ProcessPoolExecutor(multiprocessing).
concurrent.futures is backportet to Python 2.7

A look at ProcessPoolExecutor(multiprocessing)

from __future__ import print_function
from time_code import timeit
import concurrent.futures
import time

def counter(n):
    """show the count every second"""
    for k in range(n):
        print("counting {}".format(k))
        time.sleep(1)

@timeit
def main():
    with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
        for i in range(20):
            executor.submit(counter, i)

if __name__ == "__main__":
    main()

For Windows if __name__ == "__main__": and run from command line to see print function.
It can work from a IDE if no need to see output from print function.

So here max_workers=4 i got a time of 55 sec.
So if a spawn load over more processes,i should see a faster time.
Change to max_workers=30 and time go down to 19 sec.

The @timeit code i use for this.

#time_code.py
import time

def timeit(f):
    '''Timing a function'''
    def timed(*args):
        ts = time.time()
        result = f(*args)
        te = time.time()
        print 'func:{!r}  {!r} took: {:.2f} sec'.format\
        (f.__name__, args, te-ts)
        return result
    return timed
snippsat 661 Master Poster

Look at this post

EDWIN_4 commented: thanks +0
snippsat 661 Master Poster
snippsat 661 Master Poster

As mention bye chriswelborn use Flask or Bottle.
CGI is dead in Python after PEP 3333(WSGI).
Flask,Bottle is a layer above WSGI and 100% WSGI compliant.

You need some JavaScript to get the value from your input box.

JavaScript is not needed for this Flask has of course stuff like this build in with request object.

A quik demo.
HTML form(my_form.html)

<!DOCTYPE html>
<html lang="en">
<body>
    <h1>Enter some text:</h1>
    <h2>Text to be multiplied</h2>
    <form action="." method="POST">
        <input type="text" name="text">
        <input type="submit" name="my-form" value="GO">
    </form>
</body>
</html>

Flask code(form_demo.py)

from flask import Flask
from flask import request
from flask import render_template
app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template("my_form.html")

@app.route('/', methods=['POST'])
def my_form_post():
    text = request.form['text']
    multiply_text = text * 3
    return multiply_text

if __name__ == '__main__':
    app.run()

This run fine or your local computer(with Flask installed pip install Flask)
A folder with form_demo.py a subfolder templates with my_form.html
To run it in adress bar browser http://localhost:5000/
If you write hello in input box,you get back hellohellohello in browser.

use (do I need my files to be hosted to a web server)? etc.

As mention to code over work fine without a webserver in browser.
If you want to share it with world you need a webserver.
Look into Python friendly webserver as Heroku,Pythonanywhere,Openshift...

snippsat 661 Master Poster

---

snippsat 661 Master Poster

Nice, but it shortens the list if the length is not a multiple of 3.

Yes that's right,an other solution is to use map().

>>> mylist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> mylist_1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> map(None, *[iter(mylist)]*3)
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)]
>>> map(None, *[iter(mylist_1)]*3)
[(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, None)]

So here is None used as fill value.
If this this is not desirable,then other soultion in this post are fine.

snippsat 661 Master Poster

Setuptool can be found here.
Correct environment variable is ;C:\python33\;C:\python33\scripts\;
Look that's is correct by looking at Path.

snippsat 661 Master Poster

Use PyUserInput
Install Pywin32 and Pyhook
Install pip
Then do pip install PyUserInput

from pykeyboard import PyKeyboard

k = PyKeyboard()    
# pressing a key
k.press_key('H')
# which you then follow with a release of the key
k.release_key('H')
Gribouillis commented: good idea +14
entropicII commented: This helpful so far but I have no idea how to use pip which probably sounds pretty stupid so please can you just tell me how to use pip +0
snippsat 661 Master Poster

Why lambda expression?
It can make code less readable.

All you should need to do is import string and apply in to your string's first character, and string.digits, and you should be able to use that as the lambda expression.

I agree,but there is no need to import string.
All string method are always available.
import string is only needed for make alfabet.

Just a test,and when the topic is lambda expression.

>>> print((lambda x: x[0].isdigit())('1hello'))
True
>>> print((lambda x: x[0].isdigit())('hello'))
False

>>> print((lambda x: x.startswith(('1','2','3')))('2hello'))
True
>>> print((lambda x: x.startswith(('1','2','3')))('3hello'))
True
>>> print((lambda x: x.startswith(('1','2','3')))('4hello'))
False

>>> import re
>>> print((lambda x: re.match(r'\d+', x))('987hello'))
<_sre.SRE_Match object at 0x02B01D40>
>>> print((lambda x: re.match(r'\d+', x))('hello'))
None

chophouse first you make a ordinary function,because that is eaiser.
Then if that dos not do it for you look into lambda expression or functools.partial
Or the more pythonic way as PyTony suggests.

Just one more,here a generator(yield).
Wish make code memory efficiency(only 1 line in memory).

def foo():
    with open('start_digit.txt') as f:
        for line in f:
            if line[0].isdigit():
                yield line.strip()

for line in foo():
    print line
Gribouillis commented: +1 for function foo() +14
snippsat 661 Master Poster
codeList = root.findall(".//ScanCodes/ScanCode/Code")
print codeList

What dos "print codelist" output.
So here as a demo i use find() to find "value" and "text"

in [1]:    
xml = '''\
    <foo>
       <bar key="value">text</bar>
    </foo>'''

In [2]:    
import xml.etree.ElementTree as ET

In [3]:    
xml = ET.fromstring(xml)

In [4]:    
xml.find('./bar').attrib['key']   
Out[4]:
'value'

In [5]:
xml.find('./bar').text    
Out[5]:
'text'
snippsat 661 Master Poster
snippsat 661 Master Poster

That 100 line long fileBreak function is really not good at all.
You should split it up,functions should be around 10-15 lines.

Do not try to do to much in a single function.
Do a couple of task and have a clear return value.
This make code much eaiser to read and test.

snippsat 661 Master Poster

it does for the files but i jsut got told they arent files but subfolders that have the numbers in the names which makes this so much more confussing for me.

Use os.walk() it recursive scan all folder and subfolders.
Example.

import os
import re

search_pattern = r'\d'
target_path = os.path.abspath(".") #current folder
for path, dirs, files in os.walk(target_path):
    for folder_name in dirs:
        if re.findall(search_pattern, folder_name):
            print folder_name # Folder with numbers
            print(os.path.join(path, folder_name)) # Full path
snippsat 661 Master Poster

Yes is called default argument.

def say(msg, arg='something'):
    print 'first argument <{}> default argument <{}>'.format(msg, arg)

>>> say('hello')
first argument <hello> default argument <something>
>>> say('hello', 'world')
first argument <hello> default argument <world>

Many arguments.

def foo(*arg):
    return sum(arg)

>>> foo(1,2,3,4,5,6,7,8,9,10)
55  

Keyword arguments.

def bar(**kwargs):
    for key, value in kwargs.items():
        print '{}: {}'.format(key, value)

>>> bar(first_name='Clark kent', last_name='Superman')
first_name: Clark kent
last_name: Superman
snippsat 661 Master Poster

Stefan stop post your code in 8 years old Code Snippets post.
Yes Python has moved on,and your soultion is not better at all.

>>> from collections import Counter
>>> Counter('Her goes your text text'.lower().split())
Counter({'text': 2, 'goes': 1, 'her': 1, 'your': 1})
snippsat 661 Master Poster

Start by testing stuff out in interactive interpreter.

>>> lst = ['This', 'is' 'some', 'test', 'of', 'someting']
>>> lst[0]
'This'
>>> len(lst[0])
4

So useful for finding 4 letters word.
Basic iterate over a list.

>>> lst = ['This', 'is', 'some', 'test', 'of', 'someting']
>>> for item in lst:
...     print(item)
...     
This
is
some
test
of
someting

>>> for item in lst:
...     if len(item) == 4:     
...         print(item)
...         
This
some
test

>>> [item for item in lst if len(item) == 4]
['This', 'some', 'test']

Same for finding letter beginning "s" iterate over with startswith() method.
What "startswith" dos,the name pretty much reveale it.
help() works fine.

>>> help(str.startswith)
Help on method_descriptor:

startswith(...)
    S.startswith(prefix[, start[, end]]) -> bool

    Return True if S starts with the specified prefix, False otherwise.
    With optional start, test S beginning at that position.
    With optional end, stop comparing S at that position.
    prefix can also be a tuple of strings to try.
snippsat 661 Master Poster

Re upload your code with working indentation.

timetraveller1992 commented: good call. esp since it's python! +0
snippsat 661 Master Poster

And there you go, you just mentioned another way of solving the problem. There are several ways to the market in this case.

Yes there are severals ways,i was also thinking about regex,but split and startswith works just fine in this simple case.
And may even be better than regex because Readability counts.

>>> s = '20140702_PAQT_B6h'
>>> r = re.search(r'_(P.*)_(B.*)', s)
>>> r.group(1)
'PAQT'
>>> r.group(2)
'B6h'

Perl has There's more than one way to do it (TMTOWTDI)

I think Python's

There should be one-- and preferably only one --obvious way to do it.

Can always be debated,but i think soultion postet here work fine,and to debated about(obvious way) has no point in this case.

snippsat 661 Master Poster

and now please tell me the default path, that the command codecs.open() makes use of. or tell me the path of the file u saved in.. )

codecs.open() dos not use a path,you can give a path if you are not running .py in same folder as as ihij.txt.
If run .py in same folder as i did in with my test_hindi.txt i do not need to specify a path.

the file has hardly 25 words, then how can the word be repeated 172 times..

I get a count of 172(पर) with ihij.txt,wish seems correct to me.
Why do you think it's wrong?

snippsat 661 Master Poster

Import is.

>>> import xml.etree.ElementTree as ET
>>> ET.VERSION
'1.3.0'

And you should not name file elementtree.py,it shall not be in desktop folder.
This is a file in Pyhon library.

For me only Beautiful Soup or lxml for parsing.

snippsat 661 Master Poster

Hey ... sorry I missed that ... is that why you 'down-voted' ?

It was not me as downvoted you :)

snippsat 661 Master Poster

The error message is very clear,Python can not find "sample.txt" in that path.

with open('C:/Users/jolie_tia/Documents/sample.txt') as f:
    print f.name

'''Output-->
Message File Name   Line    Position
    Traceback
<module> <module4>    1
IOError: [Errno 2] No such file or directory:                               'C:/Users/jolie_tia/Documents/sample.txt'
'''

Here i fixing the error by making folders for that path and put "sample.txt" in Documents folder.

with open('C:/Users/jolie_tia/Documents/sample.txt') as f:
    print f.name

'''Output-->
C:/Users/jolie_tia/Documents/sample.txt
'''

Check your path to file again,cd to folder in Linux(terminal) an open sample.txt.
I guess you use Linux because you have not specified a drive letter.
If you use Windows,you do of course need to specifi a drive letter as i do.

Dont use file as a variable name,file is a reserved word used bye Python.

snippsat 661 Master Poster

You have convert all input to integers,so answer(ans) is also an integer.
Can look at some improvement.

num1 = int(raw_input("Enter an integer"))
num2 = int(raw_input("Enter a second integer"))
num3 = int(raw_input("Enter a third integer"))
print "The sum of the 3 integers entered is: {}".format(sum((num1, num2, num3)))

So in this version use build in sum() and string formatting.

>>> a = 1, 2, 3
>>> a
(1, 2, 3)
>>> sum(a)
6
>>> print '{} {}'.format('my score is' , sum(a))
my score is 6

If you think of it,it's really the same answer repeating 3 times.

result = []
for times in range(3):
    result.append(int(raw_input("Enter an integer: ")))
print "The sum of the 3 integers entered is: {}".format(sum(result))
ddanbe commented: Super! +15
snippsat 661 Master Poster
def lst_corrector(lst, index_vaule):
    value_change = raw_input('Enter correct value: ')
    for index, item in enumerate(lst):
        if index == index_to_change:
            lst[index] = value_change
            return lst

#Error should be car
lst = ['boat', 'train', 'carfd']
index_to_change = 2
print lst_corrector(lst, index_to_change)
#--> ['boat', 'train', 'car']
snippsat 661 Master Poster

Dont use list and dict as variable names,words are reseverd bye Python.
To clean it up.

my_file = open("words.txt")
lst = [] #Outside the loop
for word in my_file:
    word = word.strip()
    lst.append(word)
my_file.close()

#searching for the word in list
search_word = "fox"
if search_word in lst:
    print 'Word found: <{}>\nDictionary {}'.format(search_word, lst)
else:
    print "You typed a word that dosen't exist"

'''Output-->
Word found: <fox>
Dictionary ['bear', 'wolf', 'fox']
'''

Don't place all code in one big function.
Do some execerice on function,and try to keep function small and do a specific task and return result out.
This make code eaiser to read an test.

def read_contend(dictionary):
    '''Read contend and return a list'''
    with open(dictionary) as f_obj:
        word_lst = [word.strip() for word in f_obj]
        return word_lst

def search_contend(search_word, word_lst):
    '''Search for a word in input contendt'''
    if search_word in word_lst:
        return search_word
    return "You typed a word that dosen't exist"

if __name__ == '__main__':
    dictionary = 'words.txt'
    search_word = "fox"
    word_lst = read_contend(dictionary)
    print search_contend(search_word, word_lst)
snippsat 661 Master Poster

Slice it out can be another option.

with open('06 Rooster.mp3', 'rb') as f:
    f.seek(-128,2)
    tag_content = f.read(128)
    title = tag_content[3:33]
    print title # Rooster

You do know that there are several libraries that can do this(Mutagen,eyeD3...)?

import eyed3

audiofile = eyed3.load("06 Rooster.mp3")
print audiofile.tag.title
print audiofile.tag.artist
print audiofile.info.bit_rate_str

'''Output-->
Rooster
Alice In Chains
~195 kb/s
'''

Your other post.
http://www.daniweb.com/software-development/python/threads/479773/mp3-meta-data

snippsat 661 Master Poster

by having a capturing group ([a-z]), I still got 175 matches and they were just single letters

Did you place [^A-Z] as i told you?
Then it should look like this [^A-Z][A-Z]{3}([a-z])[A-Z]{3}[^A-Z]

snippsat 661 Master Poster

I have tried a written a code like so re.findall(r'[A-Z]{3}[a-z][A-Z]{3}',string) to find the solution for this python challenge

Add [^A-Z] in each end.

[^A-Z] # any character except a capital letter
[^A-Z] # ensures that there isn't a 4th uppercase letter

And parentes () around [a-z],so it's a capturing group.
This is capturing group 1 with re.findall().
You don't specific group call like you do with re.search().
re.findall() just return output from capturing group 1.

>>> import re
>>> s = 'Python is fun'
>>> re.findall(r'\w.*(fun)', s)
['fun']
>>> re.search(r'\w.*(fun)', s).group(0) #or just .group()
'Python is fun'
>>> re.search(r'\w.*(fun)', s).group(1)
'fun'
snippsat 661 Master Poster

Take a look at Click from the creator of Flask Armin Ronacher.

Gribouillis commented: Very interesting! +14
snippsat 661 Master Poster

Hint.

>>> import re
>>> data = '''(*^+*]$]+@+*_##)&)^(@$^]e@][#&)('''
>>> re.findall(r'[a-z]', data)
['e']
snippsat 661 Master Poster

One way.

f_object = open('filename', 'rb')
while True:
    chunk = f_object.read(180)
    if not chunk: break
    do_something_with(chunk)
f_object.close()

More modern an pythonic.

from functools import partial

with open('filename', 'rb') as f_object:
    for byte in iter(partial(f_object.read, 180), b''):
        # Do stuff with byte
Gribouillis commented: interesting +14
snippsat 661 Master Poster

I was thinking that if a <title> has no corresponding <pos>, delete that title, but I don't know how to do that. Can anyone suggest a solution?

Some hint use fetchNextSiblings() if return empty list,then decompose() that title tag.

xml = '''\
<page>
<title>dasher</title>
<pos>red</pos>
</page>
<page>
<title>dancer</title>
<pos>red</pos>
<pos>blue</pos>
</page>
<page>
<title>coconut</title>
</page>
<page>
<title>rudolph</title>
<pos>red</pos>
<pos>brown</pos>
<pos>red</pos>
</page>'''


from bs4 import BeautifulSoup

soup = BeautifulSoup(xml)
title = soup.find_all('title')

for index, item in enumerate(title):
    print index, item  
'''
0 <title>dasher</title>
1 <title>dancer</title>
2 <title>coconut</title>
3 <title>rudolph</title>
'''

for index, item in enumerate(title):
    print index, item.fetchNextSiblings() 
'''
0 [<pos>red</pos>]
1 [<pos>red</pos>, <pos>blue</pos>]
2 []
3 [<pos>red</pos>, <pos>brown</pos>, <pos>red</pos>]
'''

for index, item in enumerate(title):
    if item.fetchNextSiblings() == []:
        print item.decompose()
'''None'''

for index, item in enumerate(title):
    print index, item  
'''
0 <title>dasher</title>
1 <title>dancer</title>
2 <None></None>
3 <title>rudolph</title>
'''
snippsat 661 Master Poster
import os
import xml.etree.ElementTree as ET

tree = ET.parse("test.xml")
root = tree.getroot()
for element in root.iter('Path'):
    print os.path.basename(element.text)

'''Output-->
Riched32.dll
napinsp.dll
test.exe
'''

Fix so "exe" not is in output.

import os
import xml.etree.ElementTree as ET

tree = ET.parse("test.xml")
root = tree.getroot()
for element in root.iter('Path'):
    file_name = os.path.basename(element.text)
    #jpg just an example that you can have more values
    if not file_name.endswith(('.exe', '.jpg')): 
        print file_name

'''Output-->
Riched32.dll
napinsp.dll
'''
snippsat 661 Master Poster

You don't know that print element.text just was an example?
A simple test and you should be able to figure very basic stuff like this out.
You just use os.path.basename(element.text)

import os
import xml.etree.ElementTree as ET

f_out = open('my_file.txt', 'w')
tree = ET.parse("test.xml")
root = tree.getroot()
for element in root.iter('Path'):
    f_out.write('{}\n'.format(os.path.basename(element.text)))
f_out.close()
snippsat 661 Master Poster

Some hint,using a parser in standard library ElementTree.
Most of the time i use BeautifulSoup or lxml for parsing.

import os
import xml.etree.ElementTree as ET

tree = ET.parse("test.xml")
root = tree.getroot()
for element in root.iter('Path'):
    print element.text
    print os.path.basename(element.text)

'''Output-->
C:\Windows\system32\Riched32.dll
Riched32.dll
C:\Windows\system32\napinsp.dll
napinsp.dll
'''

But not duplicated .dll words

Use set()

and my .csv file

Try to do something yourself,post code if stuck.

snippsat 661 Master Poster

[::-1] or reversed()

>>> s = 'hello' 
>>> s[::-1].upper()
'OLLEH'
>>> ''.join(reversed(s.upper()))
'OLLEH'
snippsat 661 Master Poster

So it looks like it reads from all three files, but it includes the words with punctuation now?

text is now a tuple.

>>> s = 'Test.'
>>> s1 = 'hello?'
>>> s2 =  '-world?,'
>>> text = s, s1, s2 #Python always make a tuple when you do this
>>> text
('Test.', 'hello?', '-world?,')
>>> "".join(c for c in text if c not in string.punctuation).lower()
'test.hello?-world?,'
>>> text = ' '.join(text) #Fix
>>> "".join(c for c in text if c not in string.punctuation).lower()
'test hello world

Learn to do small test as i do here.

snippsat 661 Master Poster

How would I make it so that only words with 2 or more characters are put into that file?

>>> s = 'hi this is a test'
>>> s = s.split()
>>> [i for i in s if len(i) >= 2]
['hi', 'this', 'is', 'test']

#With regular loop
>>> for item in s:
...     if len(item) >= 2:
...         print item
...         
hi
this
is
test

And for the file path, if I leave it as is right now, would that work if someone else is running the program on their computer?

If no path is given,files will always be in same folder as you or other run code(.py) from.

snippsat 661 Master Poster

There are some room for improvement in this code.
In line 14 you are removing punctuation,then in line 20 you are testing for punctuation that's already removed.
There is no need to use a file and append word,just use set().
So then it could look like this.

import string

with open('alice.txt') as f:
    text = f.read().lower()

words = "".join(c for c in text if c not in string.punctuation).split()
print(len(set(words)))
snippsat 661 Master Poster

You can do this print(t[0:3] in s) read my post again.

You are checking if list ['16', '24', '30'] is in s and that is False.

And see how i make it True.

This should equal:One True for the 40 match the rest would be False:

31 40 45 57 58 - 01
01 13 21 22 40 - 31

You have to make a rule for this.
Because there are 4 elements that's in both list.

>>> s = '31 40 45 57 58 - 01'.split()
>>> t = '01 13 21 22 40 - 31'.split()
>>> [item for item in t if item in s]
['01', '40', '-', '31']

>>> #Or with set() as Grib posted
>>> ys = set(s)
>>> yt = set(t)
>>> print(ys & yt)
set(['31', '01', '-', '40'])

you can see the first number is 01 this 01 has no bearing on the sixth 01 on the first line

I guess the same is for 31,then you can take away last 2 element.

>>> s = '31 40 45 57 58 - 01'.split()[:-2]
>>> s
['31', '40', '45', '57', '58']
>>> t = '01 13 21 22 40 - 31'.split()[:-2]
>>> t
['01', '13', '21', '22', '40']
>>> [item for item in t if item in s]
['40']

>>> ys = set(s)
>>> yt = set(t)
>>> print(ys & yt)
set(['40'])

This should equal:One True for the 40 match the rest …

snippsat 661 Master Poster

Some hint.

>>> s = ['06', '15', '26', '34', '36', '-', '16']
>>> t = ['16', '24', '30', '34', '43', '-', '20']
>>> s[0]
'06'
>>> t[0]
'16'
>>> s[0] == t[0]
False
>>> s[3] == t[3]
True
>>> s[5] == t[5]
True

But I dont know why it came out <False> when <16> is in <s>

>>> t[0:3]
['16', '24', '30']
>>> t[0:3] in s
False

>>> #To make it True
>>> s = [['16', '24', '30'],'06', '15', '26', '34', '36', '-', '16']
>>> t[0:3] in s
True

You are checking if list ['16', '24', '30'] is in s and that is False.

Iterate over both list and compare.

>>> s = ['06', '15', '26', '34', '36', '-', '16']
>>> t = ['16', '24', '30', '34', '43', '-', '20']
>>> for x,y in zip(s, t):
        print x == y

False
False
False
True
False
True
False

Or make it a list,with use of comprehension.

>>> [x == y for x,y in zip(s, t)]
[False, False, False, True, False, True, False]
snippsat 661 Master Poster

Sorry but the code is not good at all.
Now you have a while True loop start at line 14 with a lot of code in it.
You have to use function or class when code get longer.

Look at code in first part here
See how function get_name() show_name() process_name() are short and give a hint what they do,this make code easy to test and read.

A single function should pretty much always be less than 15 lines,and even that is pushing it.
In your code you have all code in global space,wish make it hard to read and test.

snippsat 661 Master Poster

Good idèe bye slate to update objects namespace.
You can also just work with dictionary if you need result variable2 + variable3 wish is 30.
Herer a little compressed version.

>>> s = 'variable1=5&varible2=3&variable3=27&varible4=1'
>>> d = dict((k[0], k[1]) for k in [i.split('=') for i in s.split('&')])
>>> int(d['varible2']) + int(d['variable3'])
30