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)

print(line)

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?

Recommended Answers

All 4 Replies

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

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

or

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.

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..

commented: I didn't know pyttsx +13

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.
Example.

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.

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, :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.