python - 如何编写一个函数,它接受一个文件名并返回一个字符串,其中所有 \n 字符都替换为“_”
问题描述
我正在尝试编写一个函数,该函数采用文件名并返回一个字符串,其中所有 \n 字符都替换为“_”,但我的代码在 shell 中没有返回任何内容(没有错误),但我不确定我是什么我做错了!我们还没有学会使用'with'操作符,只是for和in。
def replace_space(filename):
text_str = ''
wordfile = open(filename)
text_str = wordfile.read()
wordfile.close()
return text_str.replace("\n", "_")
print(replace_space("roses.txt"))
解决方案
推荐阅读
- javascript - 将大型 JavaScript 对象或大型 JSON 值从 Web 服务器发送到浏览器以及 HTML 页面响应
- parsing - 将数字字符串解析为浮点数有什么风险?
- aws-lambda - 从 lambda 获取错误作为警报
- java - 如何使用 application.yml 文件指向批处理文件
- rabbitmq - org.springframework.amqp.UncategorizedAmqpException
- c# - AWS 开发工具包状态机在完成执行之前返回响应
- geocoding - 使用 Here API 获取准确的地理编码数据
- docker - 如何在 Mesos 集群中运行 flink jar 文件
- php - PHP在数字数组范围内查找气泡
- jquery - MaterlizeCSS:无法附加到选择选项