有没有一种简单的方法可以在列表中的单词中添加空格?
Is there an easy way to add spaces to words in a List?
好的,这就是我想要做的。我想轻松地将文本从文本文件转换为 word 文档。我目前有这个...
from docx import Document
text_file = "pathToYourTextFile.txt"
#opens document to add text to
document = Document()
#adds the entire contents to a list that we will
#then use to add to the document we just created
fileContents = []
for line in open(text_file):
row = line.split(' ')
fileContents += list(row)
#adds all the text we just created to the document as a paragraph
paragraph = document.add_paragraph(fileContents)
#saves the document with all the under the name we give it
document.save('test.docx')
print("Document saved.")
读取文本文件中的文本,然后将每个单词添加到列表中。然后所有单词都添加到 Document
但问题是所有单词 运行 在一起并且没有任何空格。
下面是文本的示例...
GetreadytoentertheThrivetimeshowontalk.Radio1170broadcastinglivefromthecenteroftheuniverse.It'SbusinessschoolwithouttheBSfeaturingoptometristturnedentrepreneur.Dr.RobertzoellnerwithusSBA,entrepreneuroftheYearclayClark.Dowehavecominginfromoneofourlistenersthattheyasked?Howcanyoucontrolemployeesthatyoucannotfire?HowcanyoucontrolemployeesthatyoucannotfirewellSteve?Couldyouthrowoutsomeinstanceswherethatcouldbeathingwhereyoucouldn'tfiretosuchasuper?
所以我想知道这是最好的方法吗?有更简单的方法吗?任何帮助将非常感激。先感谢您!!!
可以使用" ".join(fileContents)
,所以需要修改添加段落部分如下:
fileContents = []
for line in open(text_file):
row = line.split(' ')
fileContents += list(row)
#adds all the text we just created to the document as a paragraph
paragraph = document.add_paragraph(" ".join(fileContents))
完全不清楚您为什么要按空格拆分。如果您删除 row = line.split(' ')
并使后续行成为 fileContents += line
,您是否得到了想要的结果?您还可以通过 fileContents += '\n'
.
之前的内容来恢复换行符
你为什么把这行分成几个词?
如果你想复制所有内容,你应该使用该行(将复制 space 和新行)而不是拆分它。
所以你的代码将是:
from docx import Document
text_file = "pathToYourTextFile.txt"
#opens document to add text to
document = Document()
#adds the entire contents to a list that we will
#then use to add to the document we just created
fileContents = []
for line in open(text_file):
fileContents += line
#adds all the text we just created to the document as a paragraph
paragraph = document.add_paragraph(fileContents)
#saves the document with all the under the name we give it
document.save('test.docx')
print("Document saved.")
顺便说一句,评论不错!
编码愉快!
好的,这就是我想要做的。我想轻松地将文本从文本文件转换为 word 文档。我目前有这个...
from docx import Document
text_file = "pathToYourTextFile.txt"
#opens document to add text to
document = Document()
#adds the entire contents to a list that we will
#then use to add to the document we just created
fileContents = []
for line in open(text_file):
row = line.split(' ')
fileContents += list(row)
#adds all the text we just created to the document as a paragraph
paragraph = document.add_paragraph(fileContents)
#saves the document with all the under the name we give it
document.save('test.docx')
print("Document saved.")
读取文本文件中的文本,然后将每个单词添加到列表中。然后所有单词都添加到 Document
但问题是所有单词 运行 在一起并且没有任何空格。
下面是文本的示例...
GetreadytoentertheThrivetimeshowontalk.Radio1170broadcastinglivefromthecenteroftheuniverse.It'SbusinessschoolwithouttheBSfeaturingoptometristturnedentrepreneur.Dr.RobertzoellnerwithusSBA,entrepreneuroftheYearclayClark.Dowehavecominginfromoneofourlistenersthattheyasked?Howcanyoucontrolemployeesthatyoucannotfire?HowcanyoucontrolemployeesthatyoucannotfirewellSteve?Couldyouthrowoutsomeinstanceswherethatcouldbeathingwhereyoucouldn'tfiretosuchasuper?
所以我想知道这是最好的方法吗?有更简单的方法吗?任何帮助将非常感激。先感谢您!!!
可以使用" ".join(fileContents)
,所以需要修改添加段落部分如下:
fileContents = []
for line in open(text_file):
row = line.split(' ')
fileContents += list(row)
#adds all the text we just created to the document as a paragraph
paragraph = document.add_paragraph(" ".join(fileContents))
完全不清楚您为什么要按空格拆分。如果您删除 row = line.split(' ')
并使后续行成为 fileContents += line
,您是否得到了想要的结果?您还可以通过 fileContents += '\n'
.
你为什么把这行分成几个词? 如果你想复制所有内容,你应该使用该行(将复制 space 和新行)而不是拆分它。 所以你的代码将是:
from docx import Document
text_file = "pathToYourTextFile.txt"
#opens document to add text to
document = Document()
#adds the entire contents to a list that we will
#then use to add to the document we just created
fileContents = []
for line in open(text_file):
fileContents += line
#adds all the text we just created to the document as a paragraph
paragraph = document.add_paragraph(fileContents)
#saves the document with all the under the name we give it
document.save('test.docx')
print("Document saved.")
顺便说一句,评论不错!
编码愉快!