【发布时间】:2021-01-10 16:48:02
【问题描述】:
我有一个 txt 文件,其中包含我在 Python 中导入的文本,我想每隔 3 个单词将其分隔。
例如,
Python is an interpreted, high-level and general-purpose programming language
我想成为,
[['Python', 'is', 'an'],['interpreted,', 'high-level','and'],['general-purpose','programming','language']].
到目前为止我的代码,
lines = [word.split() for word in open(r"c:\\python\4_TRIPLETS\Sample.txt", "r")]
print(lines)
给我这个输出,
[['Python', 'is', 'an', 'interpreted,', 'high-level', 'and', 'general-purpose', 'programming', 'language.', "Python's", 'design', 'philosophy', 'emphasizes', 'code', 'readability', 'with', 'its', 'notable', 'use', 'of', 'significant', 'whitespace.', 'Its', 'language', 'constructs', 'and', 'object-oriented', 'approach', 'aim', 'to', 'help', 'programmers', 'write', 'clear,', 'logical', 'code', 'for', 'small', 'and', 'large-scale', 'projects.']]
有什么想法吗?
【问题讨论】:
-
不使用oneliner试试
-
尝试使用正则表达式。参考:stackoverflow.com/questions/41688781/…
-
这能回答你的问题吗? stackoverflow.com/questions/312443/…
标签: python arrays python-3.x list split