用python编写程序,实现读取一个英文文本文件内容,将文本中的大写字母转换为小写字母,小写字母转换为大写字母。运行效果如图1所示。
时间: 2024-02-17 21:03:38 浏览: 60
将word、pdf、txt等文档从英文翻译成中文,用Python编写。___下载.zip
好的,以下是 Python 代码实现:
```python
# 打开文件并读取内容
with open("test.txt", "r") as file:
content = file.read()
# 转换大小写
new_content = ""
for c in content:
if c.isupper():
new_content += c.lower()
elif c.islower():
new_content += c.upper()
else:
new_content += c
# 输出结果
print(new_content)
```
假设英文文本文件名为 `test.txt`,运行结果如下:
```
tHIS IS A tEST fILE.
iT cONTAINS sOME uPPER cASE lETTERS aND sOME lOWER cASE lETTERS.
wE wILL cONVERT tHE uPPER cASE lETTERS tO lOWER cASE lETTERS aND tHE lOWER cASE lETTERS tO uPPER cASE lETTERS.
hAVE fUN!
```
可以看到,所有的大写字母都被转换为小写字母,所有的小写字母都被转换为大写字母。
阅读全文