【发布时间】:2017-12-04 01:31:41
【问题描述】:
好的,问题来了。我有一些文本文件,其中包含 14,000 多个单词,但它们都在 1 行,如果您使用没有自动换行功能的编辑器,您将无法读取文本文件。因此,我想在至少 1000 个单词之后以及下一次出现"." 时将returns 或newline 字符添加到我的文件中。我的第一个想法是计算行数然后加起来,当它达到 1000 时插入一个 \n 字符,但它都在 1 行。这使事情变得更加困难,而且我一直无法找到一种方法来完成我想要的。没有我自己浏览文本文件并自己添加换行符。这违背了我的目标,即只运行一个 python 脚本来自动为我做这件事。这可能吗?还是我这么想疯了?提前感谢您提供的任何帮助!我在下面提供了我的各种尝试。
在此尝试中,代码按预期工作,但不是打印Word Count is over 1000 大约 14 次。因为,这个文本文件的字数是 14,000 等等。它只打印一次,因为它只有一行可以读取。
text_file = "textfile.txt"
numLines = 0
numWords = 0
numChars = 0
with open(text_file, 'r') as file:
for line in file:
wordsList = line.split()
numLines +=1
numWords += len(wordsList)
numChars += len(line)
if numWords > 1000:
print("Word Count is over 1000.")
在下一次尝试中,我没有做类似的事情,但仍然得到与上面相同的结果。而不是看到它将\n\n\n\n 写入文本文件大约 14 次,它只在文件末尾发生一次。
def oldWordCounter(input_file):
word_count = 0
with open(input_file, 'r') as f:
for line in f:
word_count = len(line.split(' '))
print("Word count = %s \n" % word_count)
if word_count > 1000:
with open(input_file, 'a') as f:
f.write("\n\n\n\n")
我确定我只是缺少一些简单的东西,但我对 python 还是很陌生。尽管在这里问一个问题让我很生气。我束手无策,似乎没有比这更进一步的了。因此,再次非常感谢您在此问题上提供的任何帮助!
下面我还提供了我计划在下一个周期发生后添加换行符的方式。不确定这是否会有所帮助,但可能会帮助您了解我想要完成的更多内容。
def splitOnPeriod(input_file):
with open(input_file,"r") as f:
for line in f:
searchPhrase = "."
if searchPhrase in line:
file = open(input_file, "a")
file.write("\n\n\n\n")
print("found it\n")
这是我正在处理的文本的一小部分...
World headquarters, only business Google without bada bing bada boom, guess who's back inside your room. It is the Thrive time show on your radio. My name is Clay Clark, the former and recovering disc jockey. I am joined today Inside the Box rocks with with a guy. He sees he's on telling you what he's he's back in Tulsa for at least the foreseeable future, maybe maybe for several days several minutes. It'S dr. Robert zoellner, sir welcome back. I am so fired up today. I am in such a great mood and right now I could see Marshall and I could see his reaction as I get to announce why I'm so happy all really. Yes, I glorious thing happen this weekend. You'Re discovering more hair is growing and I like you're, going with that by the way this happened to do with a little support. We Americans love so much call football Hurricane football. Absolutely I mean the world. I have waited a year to get the world right again and in my Oklahoma, Sooners go up to Columbus and whoop. I mean now. Let'S talk about the facts here, cuz there's a lot of people listening. This is a business, show its business school without the BS to keep it relevant to make sure that understand this Oklahoma. If I'm correct was right, number 5 correct and I believe that Ohio state was ranked number 2. Yes, why you leave in the box of rocks? Do is In-N-Out Marshall to the drivers who don't know Marshall, for business coaches in Ohio from Ohio and he's not so he really cares about Ohio. Yes, fifth-ranked Boomer Sooners went up there and beat him was a close. Now. It wasn't even close, really really good, and so then I'm so that was Saturday and then Sunday this last weekend and I've been waiting to have Marshall in the Box, because I can't make this announcement without you really here to sit on that till Wednesday. Clear the clear that kind of thing I didn't seem last couple things on Sunday, the Dallas Cowboys won the double bonus. Can I will quick on this and I've loved the Patriots and Jonathan are off as he hates the Patriots, and so whenever his Giants lose, I almost feel better about their loss. I almost feel better about their loss, then actual win for the Patriots and when I saw the Cowboys just turn it on I'm like this is great. I don't care what team it is as long as they're playing the Giants. I am I'm almost. I wouldn't make a prayer chain, but I will be on the verge of making your prayer chain for your team excited to see, but I don't care who it is they beat. The Giants is a great thing for American I'm a Little Lamb lunch Wagers. I am going to whenever he pays off on The Chew very slowly and enjoy every moment of tizers have reserved, but I'll have to I'll. Have I don't normally do it, but since you're paying for it Marshall, I think I will now on Today Show we're breaking down to six books that every entrepreneur should read the six books at every entrepreneur should read, and a book number one was thinking, Grow. Rich book number to you can actually get that book for free. It is start here the book The we put together the documents, our business cyst shamelessly. So if you want to learn how to grow successful company to start here to 550 page book, it's absolutely free to download it Thrive time show. And we just hit the amazon.com best sellers list on that. So if you go to Amazon now and you type in like business Consulting into the search bar, that book actually comes up in the top five books now, and so that's a book that you can get there for free to ebook, it's absolutely free for you. We move on now to book number 3, which is Titan now. Titan is the book that documents, the Life, The Life and Times of John D Rockefeller, who actually grew up like everybody else, use Easy. You start somewhere. He grew up poor and at the age of 16 he began working to support his mother because his father was an absent father and actually decided to leave his family and get married to another woman without telling his current wife it's breaking down some notable quotables from That book and I'm going to go ahead and give you the first notable quotable. This is John D. Rockefeller Miss. Is it from the book tighten the author writes he had a great generals, ability to focus on his goals and a brush aside obstacles as Petty distractions. He wants said you can abuse me.
【问题讨论】:
-
如果您可以显示一些输入文本,那么可能有一个比您尝试描述您正在寻找的解决方案更优雅的解决方案。
-
@user1767754 我已在问题底部为您添加了一些文字。
标签: python python-3.x text-files newline python-3.6