Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37964 > unrolled thread
| Started by | aramildaern@gmail.com |
|---|---|
| First post | 2013-01-30 13:16 -0800 |
| Last post | 2013-01-30 22:13 -0500 |
| Articles | 6 — 6 participants |
Back to article view | Back to comp.lang.python
help aramildaern@gmail.com - 2013-01-30 13:16 -0800
Re: help Ian Kelly <ian.g.kelly@gmail.com> - 2013-01-30 15:08 -0700
Re: help Joel Goldstick <joel.goldstick@gmail.com> - 2013-01-30 17:09 -0500
Re: help Chris Angelico <rosuav@gmail.com> - 2013-01-31 09:20 +1100
Re: help Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-31 13:42 +1100
Re: help Dave Angel <d@davea.name> - 2013-01-30 22:13 -0500
| From | aramildaern@gmail.com |
|---|---|
| Date | 2013-01-30 13:16 -0800 |
| Subject | help |
| Message-ID | <cf3b0f42-a133-45df-b1c8-8291b1896878@googlegroups.com> |
Hi everyone! I don't mean to intrude, but ive heard great things about this group and ive stumped myself with my python code.
heres my code:
#! /usr/bin/python
import sys
global labelList
labelList= dict()
global counter
counter = 0
def execute(line):
if line.find("::print") != -1:
stripPrint = line.find("::print")
startPrint = line.find('"', stripPrint)
stopPrint = line.find('"', startPrint + 1)
printSection = line[startPrint + 1 : stopPrint]
print(printSection)
if line.find("::label") != -1:
stripLabel = line.find("::label")
startLabel = line.find(' ', stripLabel)
stopLabel = line.find('--', startLabel + 1)
label = line[startLabel + 1 : stopLabel]
line.strip("\r\n")
labelList[label] = counter
if len(sys.argv) < 2:
print("error: no input files")
print("compilation terminated")
else:
fileName = sys.argv[1]
jadeFile = open(fileName, 'r')
for line in jadeFile:
counter = counter + 1
execute(line)
jadeFile.close()
i = 0
while i < len(labelList):
print(labelList.keys()[i], ":", labelList.values()[i])
i = i + 1
and its giving me a bunch of errors thanks for the help in advance!
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2013-01-30 15:08 -0700 |
| Message-ID | <mailman.1231.1359583775.2939.python-list@python.org> |
| In reply to | #37964 |
On Wed, Jan 30, 2013 at 2:16 PM, <aramildaern@gmail.com> wrote: > Hi everyone! I don't mean to intrude, but ive heard great things about this group and ive stumped myself with my python code. > > heres my code: It would be helpful if you would also include the error that you get when trying to run the code. > global labelList > labelList= dict() > > global counter > counter = 0 That said, these lines stick out as being obviously wrong. The global statement is used within the context of a function to declare that a name used within that function has global scope rather than local scope. There is no meaning to using the statement at the module level like this -- Python already knows the name is global, because it's not being used within a function -- and I'd be surprised if this didn't result in a SyntaxError.
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2013-01-30 17:09 -0500 |
| Message-ID | <mailman.1232.1359583792.2939.python-list@python.org> |
| In reply to | #37964 |
[Multipart message — attachments visible in raw view] — view raw
On Wed, Jan 30, 2013 at 4:16 PM, <aramildaern@gmail.com> wrote:
> Hi everyone! I don't mean to intrude, but ive heard great things about
> this group and ive stumped myself with my python code.
>
No intrusion. That is what the list is for.
Two points:
You should use a descriptive subject line -- "Help" isn't really
descriptive.
You should run your code, cut and paste the traceback you get showing the
errors. This is extremely useful for people to help you with, and as you
program, you will learn how useful it can be to find problems on your own.
So, do that, and come back with that info
>
> heres my code:
> #! /usr/bin/python
>
> import sys
>
> global labelList
> labelList= dict()
>
> global counter
> counter = 0
>
> def execute(line):
> if line.find("::print") != -1:
> stripPrint = line.find("::print")
> startPrint = line.find('"', stripPrint)
> stopPrint = line.find('"', startPrint + 1)
> printSection = line[startPrint + 1 : stopPrint]
> print(printSection)
>
> if line.find("::label") != -1:
> stripLabel = line.find("::label")
> startLabel = line.find(' ', stripLabel)
> stopLabel = line.find('--', startLabel + 1)
> label = line[startLabel + 1 : stopLabel]
> line.strip("\r\n")
> labelList[label] = counter
>
> if len(sys.argv) < 2:
> print("error: no input files")
> print("compilation terminated")
>
> else:
> fileName = sys.argv[1]
> jadeFile = open(fileName, 'r')
>
> for line in jadeFile:
> counter = counter + 1
> execute(line)
>
> jadeFile.close()
>
> i = 0
>
> while i < len(labelList):
> print(labelList.keys()[i], ":", labelList.values()[i])
> i = i + 1
>
>
> and its giving me a bunch of errors thanks for the help in advance!
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
Joel Goldstick
http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-01-31 09:20 +1100 |
| Message-ID | <mailman.1235.1359584417.2939.python-list@python.org> |
| In reply to | #37964 |
On Thu, Jan 31, 2013 at 8:16 AM, <aramildaern@gmail.com> wrote: > Hi everyone! I don't mean to intrude, but ive heard great things about this group and ive stumped myself with my python code. Hi! As others have said, this is no intrusion, but it'd help a lot if you posted your errors and used a more useful subject line. Additionally, when you post, can you give some example lines from the file? This part of your code is impossible for us to simulate: > fileName = sys.argv[1] > jadeFile = open(fileName, 'r') > > for line in jadeFile: > counter = counter + 1 > execute(line) > > jadeFile.close() But this much I can suggest: > i = 0 > > while i < len(labelList): > print(labelList.keys()[i], ":", labelList.values()[i]) > i = i + 1 Instead of iterating with a counter, you can iterate with a Python 'for' loop. for label,count in labelList.items(): print(label,":",count) It's that simple! ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-01-31 13:42 +1100 |
| Message-ID | <5109da11$0$29998$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #37964 |
aramildaern@gmail.com wrote: > its giving me a bunch of errors thanks for the help in advance! Yay! Guessing games! I love guessing games! Let me see if I can guess the errors you have... /usr/bin/python: bad interpreter: No such file or directory You haven't actually got Python installed, or at least not where you think it is. Am I close? If not, I recommend that you actually show us the errors you are getting. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2013-01-30 22:13 -0500 |
| Message-ID | <mailman.1241.1359602057.2939.python-list@python.org> |
| In reply to | #37964 |
On 01/30/2013 04:16 PM, aramildaern@gmail.com wrote:
> Hi everyone! I don't mean to intrude, but ive heard great things about this group and ive stumped myself with my python code.
>
> heres my code:
> #! /usr/bin/python
>
> import sys
>
> global labelList
> labelList= dict()
>
> global counter
> counter = 0
>
> def execute(line):
> if line.find("::print") != -1:
> stripPrint = line.find("::print")
> startPrint = line.find('"', stripPrint)
> stopPrint = line.find('"', startPrint + 1)
> printSection = line[startPrint + 1 : stopPrint]
> print(printSection)
>
> if line.find("::label") != -1:
> stripLabel = line.find("::label")
> startLabel = line.find(' ', stripLabel)
> stopLabel = line.find('--', startLabel + 1)
> label = line[startLabel + 1 : stopLabel]
> line.strip("\r\n")
> labelList[label] = counter
>
> if len(sys.argv) < 2:
> print("error: no input files")
> print("compilation terminated")
>
> else:
> fileName = sys.argv[1]
> jadeFile = open(fileName, 'r')
>
> for line in jadeFile:
> counter = counter + 1
> execute(line)
>
> jadeFile.close()
>
> i = 0
>
> while i < len(labelList):
> print(labelList.keys()[i], ":", labelList.values()[i])
> i = i + 1
>
>
> and its giving me a bunch of errors thanks for the help in advance!
>
davea@think2:~/temppython$ python aram.py
error: no input files
compilation terminated
These messages are triggered by sys.argv being less than 2. Cure is to
pass some string as the first argument on the command line.
Fixed that:
davea@think2:~/temppython$ ./aram.py myfile.txt
Traceback (most recent call last):
File "./aram.py", line 33, in <module>
jadeFile = open(fileName, 'r')
IOError: [Errno 2] No such file or directory: 'myfile.txt'
(notice that I pasted the full traceback into this message.)
Problem is that the program is treating that parameter as a filename,
and I don't have a file by that filename.
davea@think2:~/temppython$ ./aram.py aram.py
) != -1:
)
(' stripLabel = line.find("::label")', ':', 20)
('!= -1:', ':', 19)
Worked perfectly. Of course, nobody has said what it's supposed to do.
So anything that doesn't display an error must be okay.
How about describing what version of Python this is intended for, how
you ran it, and what errors you're getting. Don't paraphrase, don't
summarize (lots of errors ???!), just copy/paste.
And if it's appropriate, show a sample data file for it to open, not
attached, but pasted into your email message.
--
DaveA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web