python - 是否可以将列表导出到 .txt 以便记事本可以读取换行符?
问题描述
出于可访问性的原因,我想知道在 python 中是否可以将列表导出到 .txt 以便记事本可以读取换行符?下面是一个在记事本++中正确读取但在记事本中不正确的示例代码。在记事本++中,列表的每个条目都在单独的行上,在记事本中,所有条目都在同一行上。
string =['str1 123','str2 234','str3 345']
outF = open("outp.txt", "w")
for item in string:
outF.write("%s\n" % item)
outF.close()
解决方案
Windows 使用C arriage R eturn, Line Feed :来表示换行符\r\n
,这是 Windows 记事本唯一识别的换行符:
In [7]: s = ['hello', 'world']
In [8]: with open('test.txt', 'w') as f:
...: for item in s:
...: f.write('%s\r\n' % item)
例子:
基于 Linux 的系统使用Line Feed来指示换行符,而旧的 Mac OS 过去只使用C arriage R eturn ,并且像 Notepad ++这样的编辑器可以配置为识别所有这些,而记事本则不能。
推荐阅读
- mysql - Mysql - 分组并选择最大值
- vb.net - 没有密码的 Itextsharp 加密无法正常工作
- javascript - 如何使用 javascript DOM 获取元素 P 中的文本,不包括其可能的子元素 A?
- coq - `le` 的归纳原理
- html - HTML iframe 允许自动播放音频文件不工作 safari
- r - 为什么mapply不在输出中存储ggplot对象
- python - 在 localhost:8080 中运行 HTML 文件
- python - 如何获取在场景中创建的根父级列表 - Autodesk Maya / Python?
- class - 如何在 kotlin 中初始化一个类数组?
- javascript - 来自服务器端的 JSON 字符串无法正确使用 ajax jquery