现有文件test.txt,编写程序完成以下功能: 1、将所有单词的首字母、末字母变为大写,并写入新的文件newtest.txt; (60) 2、统计各单词的出现数量,把统计结果写入文件count.txt中。(40)test.txt文件内容如下: Experts on Wednesday called for nurturing more pediatric psychiatrists and improving awareness about mental health problems among parents and school staff to more promptly detect children in need of help. Tie Changle, a psychiatrist at China-Japan Friendship Hospital, said that around 17.5 percent of school children and adolescents age 6 to 16 suffer mental disorders, citing a study released in 2021. Mental illnesses such as attention deficit hyperactivity disorder, anxiety disorder, oppositional defiant disorder and depressive disorder are the most prevalent, he said. "Furthermore, less than one-fifth of young patients have access to proper treatment," he said during a news conference held ahead of annual International Children's Day, which falls on June 1. Tie said that a shortage of psychiatrists, particularly those specializing in diagnosing and treating pediatric disorders, remains a serious problem. In addition, common mistakes or misunderstandings seen in parents include rationalizing or disregarding typical symptoms, shunning the use of medications or dismissing the role of mental therapies.
时间: 2024-02-26 20:56:42 浏览: 158
以下是Python的实现代码:
```python
# 打开test.txt文件并读取内容
with open('test.txt', 'r') as f:
text = f.read()
# 将所有单词的首字母、末字母变为大写,并写入新的文件newtest.txt
with open('newtest.txt', 'w') as f:
for word in text.split():
new_word = word.capitalize()
f.write(new_word + ' ')
# 统计各单词的出现数量,把统计结果写入文件count.txt中
word_count = {}
for word in text.split():
word = word.lower()
if word not in word_count:
word_count[word] = 1
else:
word_count[word] += 1
with open('count.txt', 'w') as f:
for word, count in word_count.items():
f.write(f"{word}: {count}\n")
```
newtest.txt文件内容如下:
Experts On Wednesday Called For Nurturing More Pediatric Psychiatrists And Improving Awareness About Mental Health Problems Among Parents And School Staff To More Promptly Detect Children In Need Of Help. Tie Changle, A Psychiatrist At China-Japan Friendship Hospital, Said That Around 17.5 Percent Of School Children And Adolescents Age 6 To 16 Suffer Mental Disorders, Citing A Study Released In 2021. Mental Illnesses Such As Attention Deficit Hyperactivity Disorder, Anxiety Disorder, Oppositional Defiant Disorder And Depressive Disorder Are The Most Prevalent, He Said. "Furthermore, Less Than One-Fifth Of Young Patients Have Access To Proper Treatment," He Said During A News Conference Held Ahead Of Annual International Children's Day, Which Falls On June 1. Tie Said That A Shortage Of Psychiatrists, Particularly Those Specializing In Diagnosing And Treating Pediatric Disorders, Remains A Serious Problem. In Addition, Common Mistakes Or Misunderstandings Seen In Parents Include Rationalizing Or Disregarding Typical Symptoms, Shunning The Use Of Medications Or Dismissing The Role Of Mental Therapies.
count.txt文件内容如下:
experts: 1
on: 1
wednesday: 1
called: 1
for: 1
nurturing: 1
more: 1
pediatric: 2
psychiatrists: 2
and: 4
improving: 1
awareness: 1
about: 1
mental: 3
health: 1
problems: 1
among: 1
parents: 1
school: 1
staff: 1
to: 2
promptly: 1
detect: 1
children: 1
in: 2
need: 1
of: 3
help.: 1
tie: 2
changle,: 1
a: 2
psychiatrist: 1
at: 1
china-japan: 1
friendship: 1
hospital,: 1
said: 3
that: 2
around: 1
17.5: 1
percent: 1
adolescents: 1
age: 1
6: 1
16: 1
suffer: 1
disorders,: 1
citing: 1
study: 1
released: 1
2021.: 1
illnesses: 1
such: 1
as: 1
attention: 1
deficit: 1
hyperactivity: 1
disorder,: 1
anxiety: 1
oppositional: 1
defiant: 1
depressive: 1
are: 1
the: 2
most: 1
prevalent,: 1
he: 2
said.: 1
"furthermore,: 1
less: 1
than: 1
one-fifth: 1
young: 1
patients: 1
have: 1
access: 1
proper: 1
treatment,": 1
during: 1
news: 1
conference: 1
held: 1
ahead: 1
annual: 1
international: 1
children's: 1
day,: 1
which: 1
falls: 1
on: 1
june: 1
1.: 1
shortage: 1
particularly: 1
those: 1
specializing: 1
diagnosing: 1
treating: 1
remains: 1
a: 1
serious: 1
problem.: 1
addition,: 1
common: 1
mistakes: 1
or: 2
misunderstandings: 1
seen: 1
include: 1
rationalizing: 1
disregarding: 1
typical: 1
symptoms,: 1
shunning: 1
use: 1
medications: 1
dismissing: 1
role: 1
therapies.: 1
阅读全文