help on encoding

Sockets, TCP/IP, (XML-)RPC und ähnliche Themen gehören in dieses Forum
Antworten
reverse
User
Beiträge: 62
Registriert: Mittwoch 29. November 2006, 22:43

Hi all
I am trying to play youtube video using vlc....based on code by following
site http://thinkhole.org/wp/2006/01/09/the- ... nd-python/
and I get following error
>C:\Python25\pythonw -u "youtubeApi.py"
File "youtubeApi.py", line 15
SyntaxError: Non-ASCII character '\x92' in file youtubeApi.py on line 15, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
can someone help me where I should start....I visited http://www.python.org/peps/pep-0263.html but made me
even feel more dizzy.

thanks
reverse
BlackJack

There's a non-ASCII character in your source file. Either remove the offending character or add the "coding comment" at the top the file to tell Python what encoding is used in that source file.
reverse
User
Beiträge: 62
Registriert: Mittwoch 29. November 2006, 22:43

hi blackjack
how to do this?

Code: Alles auswählen

import sys
sys.setdefaultencoding('utf-8')
I tried but didn`t worked


the whole code

Code: Alles auswählen

#!/usr/bin/python
#Load continues video from youtube.com.
#Requires videolan (VLC) media player (see http://www.videolan.org/)
#Just put this python script in your vlc.exe directory.
import urllib,sys
import os
import sys #  ========== changes made here ==========
sys.setdefaultencoding('utf-8')  #  ========== changes made here ==========
from time import sleep
from string import find,replace
from htmllib import HTMLParser
from formatter import AbstractFormatter,DumbWriter
from cStringIO import StringIO
from random import shuffle

number_of_videos=30
startpage=’http://www.youtube.com/categories_portal?c=23&e=1'

def readurl(url):
try:
f=urllib.urlopen(url)
txt=f.read()
f.close()
except:
txt='’
return txt

def FindLinks(html):

tmp = StringIO()
f = AbstractFormatter(DumbWriter(tmp))
parser=HTMLParser(f)
parser.feed(html)
return parser.anchorlist

txt=readurl(startpage)
linklist=FindLinks(txt)
shuffle(linklist)
started=False
cnter=999
linklist2=[]
print ‘loading link pages, please wait…’
for startpage in linklist:
if find(startpage,’/watch’)==-1 and number_of_videos>len(linklist2):
txt=readurl(’http://www.youtube.com’+startpage)
ll=FindLinks(txt)
for l in ll:
if find(l,’/watch’)>-1:
if not l in linklist2:
linklist2.append(l)
else:
if not startpage in linklist2:
linklist2.append(startpage)
shuffle(linklist2)
for startpage in linklist2:
txt=readurl(’http://www.youtube.com’+startpage)
spot=find(txt,’new SWFObject’)
if spot>-1:
cnter+=1
txt=txt[spot+37:]
spot2=find(txt,’”‘)
txt=txt[:spot2]
video_id = txt[:]
fname=’test’+str(cnter)+’.flv’
print ‘retrieving video::’+str(cnter)

urllib.urlretrieve(”http://www.youtube.com/get_video?video_id=”+video_id,fna
me)
if started:
o.close()
o=os.popen(”vlc.exe “+fname+” -f vlc:quit”)
started=True
EnTeQuAk
User
Beiträge: 986
Registriert: Freitag 21. Juli 2006, 15:03
Wohnort: Berlin
Kontaktdaten:

I think this is what BlackJack meaning with
add the "coding comment" at the top the file to tell Python what encoding is used in that source file.
The follow script should run without a error:
(untested :D )

Code: Alles auswählen

#!/usr/bin/python
# -*- coding: utf-8 -*-

#Load continues video from youtube.com.
#Requires videolan (VLC) media player (see http://www.videolan.org/)
[...]

That should work ;)
reverse
User
Beiträge: 62
Registriert: Mittwoch 29. November 2006, 22:43

hello EnTeQuAk
I tried that method too but it still throws so many syntax errors.

anyway using # wouldn`t it comment out the whole # -*- coding: utf-8 -*- thing? though I tried both ways.


do you have vlc installed? would be great to see if it works for you!


anyway thanks man!!!!
reverse
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

reverse hat geschrieben:I tried that method too but it still throws so many syntax errors.
Yeah, thats because Python-Blocks are marked with indentation. Do you see any indentation in your code? No? That's why you get SyntaxErrors.
reverse hat geschrieben:anyway using # wouldn`t it comment out the whole # -*- coding: utf-8 -*- thing? though I tried both ways.
No, it does not, as -*- coding: utf-8 -*- is not valid Python-source. This a some kind of magic-commentary, just like the shebang.

So you just need to fix the indentation and you're done.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
reverse
User
Beiträge: 62
Registriert: Mittwoch 29. November 2006, 22:43

hi Leonidas

I was using scite but change to built in python editor IDLE to fix the indentation have no indentation error but get this error

***'return outside function (filename), line31)

line31 =return parser.anchorlist ......why?????

once again code below with fixed indentation.

Code: Alles auswählen

#!/usr/bin/python
# -*- coding: utf-8 -*-
#Load continues video from youtube.com.
#Requires videolan (VLC) media player (see http://www.videolan.org/)
#Just put this python script in your vlc.exe directory.
import urllib,sys
import os
from time import sleep
from string import find,replace
from htmllib import HTMLParser
from formatter import AbstractFormatter,DumbWriter
from cStringIO import StringIO
from random import shuffle

number_of_videos=30
startpage="http://www.youtube.com/categories_portal?c=23&e=1"

def readurl(url):
	try:
		f=urllib.urlopen(url)
		txt=f.read()
		f.close()
	except:
			txt=""
			return txt
def FindLinks(html):
	tmp = StringIO()
f = AbstractFormatter(DumbWriter(tmp))
parser=HTMLParser(f)
parser.feed(html)
return parser.anchorlist

txt=readurl(startpage)
linklist=FindLinks(txt)
shuffle(linklist)
started=False
cnter=999
linklist2=[]
print "loading link pages, please wait…"
for startpage in linklist:
	if find(startpage,"/watch")==-1 and number_of_videos>len(linklist2):
		txt=readurl("http://www.youtube.com"+startpage)
ll=FindLinks(txt)
for l in ll:
	if find(l,"/watch")>-1:
		if not l in linklist2:
			linklist2.append(l)
else:
	if not startpage in linklist2:
		linklist2.append(startpage)
		shuffle(linklist2)
for startpage in linklist2:
	txt=readurl("http://www.youtube.com"+startpage)
	spot=find(txt,"new SWFObject")
	if spot>-1:
		cnter+=1
		txt=txt[spot+37:]
spot2=find(txt,"”")
txt=txt[:spot2]
video_id = txt[:]
fname="test"+str(cnter)+".flv"
print "retrieving video::"+str(cnter)

urllib.urlretrieve("http://www.youtube.com/get_video?video_id=”+video_id,fname")
if started:
	o.close()
	o=os.popen("vlc.exe “+fname+” -f vlc:quit")
	started=True
thanks
reverse
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

reverse hat geschrieben:***'return outside function (filename), line31)

line31 =return parser.anchorlist ......why?????
Because the code starting in line 28 is not indented properly (it is not indented at all, so to say).

Maybe you could post your code into the LodgetIt, because we are experiencing some problems with longer source codes in this forum.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
BlackJack

reverse hat geschrieben:I was using scite but change to built in python editor IDLE to fix the indentation have no indentation error but get this error

***'return outside function (filename), line31)

line31 =return parser.anchorlist ......why?????

once again code below with fixed indentation.
No the indentation is not really fixed. Indentation is *meaningful* in Python. The ``return`` statement is used within functions so it has to go into a functions body but you have one at module level on line 31.

Maybe you should work through the Python tutorial before continuing this program.
reverse
User
Beiträge: 62
Registriert: Mittwoch 29. November 2006, 22:43

hi all
is there any other third party programme that helps to fix Indentation or keyboard combination?

my current python properties in scite.
statement.indent.$(file.patterns.py)=10 :
statement.end.$(file.patterns.py)=
statement.lookback.$(file.patterns.py)=0
block.start.$(file.patterns.py)=
block.end.$(file.patterns.py)=

tabsize=8
indent.size=4
use.tabs=0
indent.automatic=1
indent.opening=1
indent.closing=0
tab.indents=1
backspace.unindents=1
tab.timmy.whinge.level=1
I read the tutorials but still don`t get the logic?
BlackJack

reverse hat geschrieben:hi all
is there any other third party programme that helps to fix Indentation or keyboard combination?
Not really because indentation carries the programmers intent and computers can't read your mind.
I read the tutorials but still don`t get the logic?
If you define a function, write loops or ``if``/``else`` statements you have one or more statements that belong to that function, loop or decision branch. Those statements are "marked" by the same indention level. Otherwise Python does not know where the block of statements end.
reverse
User
Beiträge: 62
Registriert: Mittwoch 29. November 2006, 22:43

Hi black jack and everyone
If you define a function, write loops or ``if``/``else`` statements you have one or more statements that belong to that function, loop or decision branch. Those statements are "marked" by the same indention level. Otherwise Python does not know where the block of statements end.
I read the tutorials and indentation and still helpless.
I just can`t find any logic, that would define the set of rules, since python dont use symbols like (){}[] for beginning and the ending of certain function, how will I know when and which line of code should appear on the right indentation.

in hope to get things right I changed the editor to notepad++ but still with no success...are there any debugger, which will help me to solve line by line?


if not I would appericiate a lot , if any one of you would upload a py file so that I can look and learn...please!

many thanks
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

reverse hat geschrieben:I read the tutorials and indentation and still helpless.
I just can`t find any logic, that would define the set of rules, since python dont use symbols like (){}[] for beginning and the ending of certain function, how will I know when and which line of code should appear on the right indentation.
You could write a mail to the autor and ask him to send you a properly indented version.
reverse hat geschrieben:if not I would appericiate a lot , if any one of you would upload a py file so that I can look and learn...please!
Yeah, sure, here you have lots of them.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
lunar

reverse hat geschrieben: I read the tutorials and indentation and still helpless.
I just can`t find any logic, that would define the set of rules, since python dont use symbols like (){}[] for beginning and the ending of certain function, how will I know when and which line of code should appear on the right indentation.
Did you never indent code in other languages? :shock:
If you did, just indent code the same way you did in other languages, but leave the symbols. Then you have it...
If you have never indented code before, than python isn't probably the right language for you ;)
Anyway, you start a new indented block in python, where you would place an open curly brace in C. You stop indenting code at that point, where you place a closing curly brace in C.

Code: Alles auswählen

function hello( void ) 
{ // in python you would start indenting here
printf('Hello world')
} // in python you would stop indenting here
hello()
The same in python:

Code: Alles auswählen

def hello(): # same as function hello( void ) in c
    # start indenting here (with 4 spaces)
    print 'hello world'
# stop indenting here. 
# Code outside the function (behind the curly brace in C) is not indented
hello()
reverse hat geschrieben:if not I would appericiate a lot , if any one of you would upload a py file so that I can look and learn...please!
You could also take a look at the source of python itself...

hope it helps
lunar
Antworten