Hi, Im new to programming and to Python. Im making a program to get weather information from online, parse it the way I want to, then have Festival read it aloud to me. I got the weather info and parsing done exactly how I want but am getting an error while having Festival read it back to me. I haven't had any problems with festival before and use similar code with no problem. I think it may have to do with the data type some how. Here is the code followed by the error message:

import subprocess
os.system('modprobe pcspkr')

logfile = open("weather.txt", "r").readlines()

here I got a specific line from file with my weather info

line = logfile[34]
print (line)

Here I parsed it and replaced words with other words to make a sentence for Festival to speak

line = line.replace("-", "will be")
line = line.replace("<br />", "")
line = line.replace(":", " temperature is,")
line = line.replace("Low", ", Low ")

And here is the problem area which will work if I replace the "line" variable with just about anything else

subprocess.call('echo '+line+' | festival --tts', shell=True)


Here is the output when I run program

/bin/sh: 2: Syntax error: "|" unexpected
Wed will be Mostly Sunny. High temperature is, 54 , Low temperature is, 34

Any idea of whats going on here?

5 Years
Discussion Span
Last Post by mmpal78

Have you tried using either string interpolation or the .format() method instead of string concatenation?

subprocess.call('echo %s | festival --tts' % (line), shell=True)


subprocess.call('echo {0} | festival --tts'.format(line), shell=True)

Also, if you are willing to use espeak instead of Festival, you might consider using PyTTSx rather than shelling out to the system.

Edited by Schol-R-LEA


OK, I tried out my own suggestion, and found that it didn't solve the problem. However, I did determine that putting double-quotes around the text does get it to work:

subprocess.call('echo "{0}" | festival --tts'.format(line), shell=True)

I'd still recommend using PyTTSx, though, if it is an option for you..

Votes + Comments
I didn't know pyttsx

You can read direct from web,then you dont have to save and read from txt.
This way you always get uppdatet info when website change.

from bs4 import BeautifulSoup

#Use urllib to read html source code
html = '<b>Mostly Cloudy, 50 F</b>'

soup = BeautifulSoup(html)
tag = soup.find('b')
weather_info = tag.text.split(',')

#Text that can go into speak engine
print 'Today`s weather will be {} and temprature of{}.'.format(weather_info[0], weather_info[1])
#--> Today`s weather will be Mostly Cloudy and temprature of 50 F.

Edited by snippsat


Thanks everyone for responding. I did neeed to put it in ""'s and did by
line = '"%s"' % line
I will check out the BeautifulSoup method too. Im glad to know I wasn't the only geek programming on Turkey Day, :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.