删除单引号,逗号

时间:2014-03-26 09:41:57

标签: string list python-2.7 ipython-notebook

我列出了以下单词的内容。

      word_list = ['Here', 'is', 'an', 'example', 'of', 'what', 'I', 'am',
                   'working', 'with', 'But', 'I', 'need', 'to', 'change', 'the format',
                   'to', 'something', 'more', 'useable']

我想这种格式

     word_list = [Here is an example of what I am
                    working with But I need to change the format 
                    to something more useable]

有可能实现这一目标吗?如何连接列表中的单词?

我试过这段代码。但我实现了一个单词列表而不是一个句子。

  blog_posts=[]
  stop = stopwords.words('english')+[
   '.',
   ',',
   '--',
   '\'s',
    '?',
    ')',
   '(',
   ':',
  '\'',
   '\'re',
  '"',
  '-',
  '}',
  '{',
  u'—',
 'a', 'able', ]
 file=open("resources/ch05-webpages/newspapers/25314/Indexpress111.csv","r+")
 #resources\ch05-webpages\newspapers\midday11.csv
 #t=[i for i in file.read().split() if i not in stop]
 t=    [i.replace("'","").replace("?","").replace(":","").replace("\"","").replace("\'","").strip() 

for i in file.read().split() if i not in stop]
 blog_posts.append((t,))
print blog_posts

1 个答案:

答案 0 :(得分:0)

首先,您的word_list是一个列表。如果要将连接的单词作为字符串输出,可以使用str.join(list),请参阅Explain Python .join()。我假设您所需的输出是一个可打印到控制台或可写入文件的字符串,因此您可以尝试:

>>> word_list = ['Here', 'is', 'an', 'example', 'of', 'what', 'I', 'am',
...                    'working', 'with', 'But', 'I', 'need', 'to', 'change', 'the format',
...                    'to', 'something', 'more', 'useable']
>>> 
>>> print " ".join(word_list)
Here is an example of what I am working with But I need to change the format to something more useable

如果上一段似乎与你陌生,那么我建议你在python中查看一些基础知识,然后回到上面的代码片段来理解它,试试http://www.codecademy.com/tracks/python