0

I am writing a simple Python script that retrieves the latest tweet of any twitter user (in this case BBC) and uses the integrated text-to-speech system on Mac to read out the content of that particular tweet.

Everything is running as it should, but there are certain things I want to improve. For instance, if a tweet contains the character "#", the computer will speak this as "number". E.g, if the tweet were to read "#BBC covers the latest news", the computer speaks "number BBC covers the latest news".

I have declared a string to hold the content of the tweet, and wish to find a way to replace unwanted characters with white spaces. So far, I have the following:

for char in data_content: #data_content is the string holding the tweet
    if char in "#&/": # does not replace #
        mod_data = data_content.replace(char, '')
print(mod_data)
system('say ' + mod_data)

This seems to be working correctly with the "/" character, but does not replace the "#" character. So, any help on this matter is very much appreciated!

P.S. I have tried replacing the "#" character alone, in which case I get the desired result. However, when I try to provide a series of characters to replace, it only replaces the "/" character.

Thanks!

4

2 に答える 2

1

ループは常に data_content を mod_data に変換するため、常に最後の変更のみが表示されます。

あなたの文字列が"#BBC covers the latest issues with G&F. See bbc.co.uk/gf"

リスト内の文字が最初に見つかったのは次の#とおりです。

mod_data = "BBC covers the latest issues with G&F. See bbc.co.uk/gf"

次に&が見つかりましたが、data_content で見つかったため、以前に行った変更は無視され、次のようになります。

mod_data = "#BBC covers the latest issues with GF. See bbc.co.uk/gf"

が見つかった場合も同じことが起こり、次の/ようになります。

mod_data = "#BBC covers the latest issues with G&F. See bbc.co.ukgf"

そのため、 でのみ機能しているように見えます/

次のような正規表現を使用して、必要なことを簡単に行うことができます。

import re

string = "#BBC covers the latest issues with G&F. See bbc.co.uk/gf"
mod_data = re.sub(r"[#&/]", " ", string)
print(mod_data)
system('say ' + mod_data)
于 2013-05-14T22:41:46.173 に答える
0

追加の提案があります。文字列内の文字のすべての出現に対して機能するためreplace()、その外側のループは必要ないため、コードを次のように変更できます。

mod_data = data_content
for char in "#&/":
    mod_data = mod_data.replace(char, '')
于 2013-05-14T22:53:50.223 に答える