python:多行用户输入,上一行的最后一个单词连接到下一行的第一个单词



该代码应该创建一个字典,用于计算文本正文中单词的出现次数。这是使用 While 循环、尝试阻止与 EOFError 异常和Ctrl+D当程序完成文本循环时完成。

from collections import Counter

print("Enter/Paste your multiline / paragraph of text. Hit Ctrl + D to run it ")
print("DO NOT PRESS ENTER OR CTRL+V AFTER THE FIRST TIME YOU DROP IN AND RUN YOUR TEXT ")
#DO NOT PUT IN TEXT IN input() TEXT WILL JUST APPEAR OVER AND OVER AGAIN AND LOOK ANNOYING
work_on_this_string_built_from_loop = "" # this initializes out of the loop and adds 
# text from input as a single lined string
while True: # the loop will always = True so this creates an infinite loop
print("DO NOT PRESS ENTER OR CTRL+V AFTER YOU HAVE INITIALLY RAN YOUR TEXT ")
print("INSTEAD HIT CTRL + D TO RUN IT AFTER TEXT IS INPUTTED")
print()
print("THE OUTPUT ABOVE IS NOT THE FINAL RESULT IT IS NOT DONE YET")
print("IT IS ONLY  OUTPUT FROM THE FIRST PARAGRAPH OR LINE BREAK ")
try:
multi_lined_input = input("n") # blank screen of input will appear after text is pasted in
work_on_this_string_built_from_loop+=multi_lined_input
print(f"line is equal to {work_on_this_string_built_from_loop} ") # INPUTS FILE/USER INPUT AS A SINGLE LINED STRING
print()
print()
print(work_on_this_string_built_from_loop.split('n'))

frequency_of_words_dict = dict(  Counter( work_on_this_string_built_from_loop.split() )  )

print()

except EOFError as error : # this allows for quiting the program using CTRL + D
# once the error is excepted, the program ends and this
#causes the break statement to execute and the loop ends
print("ITERATING THROUGH EXCEPTION")
print(frequency_of_words_dict)
for i in frequency_of_words_dict.items():
print(i)
break

print("PROGRAM COMPLETE")
print(f"the length of words in entry is : {len(work_on_this_string_built_from_loop.split())}")

但是当我将文本复制并粘贴到程序中时,它会将一行的最后一个单词与它后面的行的第一个单词连接起来。

例:

Example string used below
You may charge a reasonable fee for copies of or *providing
access* to or distributing Project Gutenberg-tm electronic works 
provided that
Example Output: 
{'You': 1, 'may': 1, 'charge': 1, 'a': 1, 'reasonable': 1, 'fee': 1, 
'for': 1, 'copies': 1, 'of': 1, 'or': 2,
'*providingaccess*': 1, 'to': 1, 
'distributing': 1, 'Project': 1, 
'Gutenberg-tm': 1, 'electronic': 1, '*worksprovided*': 1, 'that': 1}
('You', 1)
('may', 1)
('charge', 1)
('a', 1)
('reasonable', 1)
('fee', 1)
('for', 1)
('copies', 1)
('of', 1)
('or', 2)
('*providingaccess*', 1)
('to', 1)
('distributing', 1)
('Project', 1)
('Gutenberg-tm', 1)
('electronic', 1)
('*worksprovided*', 1)
('that', 1)

如何拆分上一行的最后一个单词和下一行的第一个单词?

处理输入时,将删除换行符。 将行追加到work_on_this_string_built_from_loop时,追加一个空格。 如果您担心多余的空间,请检查该条件。

work_on_this_string_built_from_loop +=     
multi_lined_input + ('' if multi_lined_input == ' ' else ' ')

相关内容

  • 没有找到相关文章

最新更新