格式化从文件中捕获的随机行时出现问题
Having problem formatting random lines captured from a file
此代码 returns 来自文件的随机 3 行。它们成套返回,我想以可展示的格式打印出来。你会怎么做?
这是代码。
import random
with open("/Users/will/Dropbox/zettelkasten/Super Slogans 202012281549.md") as file_in:
lines = []
for line in file_in:
lines.append(line.replace(u'\xa0', u' '))
sample = (f' {random.sample(lines, 3)}')
print(sample)
原始文件看起来像。
- Divided focus leads to divided results.
- Where I point my attention becomes how I spend my life.
- What is natural is not supernatural.
and 602 other lines.
这是我得到的输出。
['- Divided focus leads to divided results.\n', '- Where I point my attention becomes how I spend my life.\n', '- What is natural is not supernatural.\n']
这是所需的输出。
- Divided focus leads to divided results.
- Where I point my attention becomes how I spend my life.
- What is natural is not supernatural.
我试过没有成功
print(*sample, end="\n ")
print(*sample, sep="\n")
print('\n'.join(sample))
尝试:
f = open("/Users/will/Dropbox/zettelkasten/Super Slogans 202012281549.md", 'r')
sample = f.read()
print(sample)
我怀疑你有一些奇怪的行尾。
这对我来说效果很好,并且使用了您尝试过的打印语句之一:
import random
with open("test.txt") as file_in:
lines = []
for line in file_in:
lines.append(line[:-1])
sample = random.sample(lines, 3)
print('\n'.join(sample))
test.txt
中的内容:
- Divided focus leads to divided results.
- Where I point my attention becomes how I spend my life.
- What is natural is not supernatural.
- Divsasdided focus leads to divided results.
- Where I point my attadasdntion becomes how I spend my life.
- What is natural is dasdanot supernatural.
- Diviadsadded focus leaasadaads to divided results.
- Where I point myadasd attention becomes hasdaow I spend my life.
- What is naturaladasd is not supernatural.
- Dividedassassss focus leads to dividesssd results.
- Whersssse I point my attention becomes hosssssw I spend my life.
- Whdsdasfasfaat is natural is not dfgsdfsdfsdsupernatural.
在从 here 启动的 Jupyter 环境中使用该代码和文本文件内容进行尝试,以分离出您首先要进行的机器或特定文件。
append 语句中的切片删除了行尾。如果您涉及奇怪的行结尾,这可能对您有用。如果你有典型的行尾,你可以只使用 strip()
方法,即,为了附加使用 lines.append(line.strip())
.
通过将行尾添加回最后 join
,您可以避免处理可能合并的最后一行,因为它没有行尾而其他行有。
此代码 returns 来自文件的随机 3 行。它们成套返回,我想以可展示的格式打印出来。你会怎么做?
这是代码。
import random
with open("/Users/will/Dropbox/zettelkasten/Super Slogans 202012281549.md") as file_in:
lines = []
for line in file_in:
lines.append(line.replace(u'\xa0', u' '))
sample = (f' {random.sample(lines, 3)}')
print(sample)
原始文件看起来像。
- Divided focus leads to divided results.
- Where I point my attention becomes how I spend my life.
- What is natural is not supernatural.
and 602 other lines.
这是我得到的输出。
['- Divided focus leads to divided results.\n', '- Where I point my attention becomes how I spend my life.\n', '- What is natural is not supernatural.\n']
这是所需的输出。
- Divided focus leads to divided results.
- Where I point my attention becomes how I spend my life.
- What is natural is not supernatural.
我试过没有成功
print(*sample, end="\n ")
print(*sample, sep="\n")
print('\n'.join(sample))
尝试:
f = open("/Users/will/Dropbox/zettelkasten/Super Slogans 202012281549.md", 'r')
sample = f.read()
print(sample)
我怀疑你有一些奇怪的行尾。
这对我来说效果很好,并且使用了您尝试过的打印语句之一:
import random
with open("test.txt") as file_in:
lines = []
for line in file_in:
lines.append(line[:-1])
sample = random.sample(lines, 3)
print('\n'.join(sample))
test.txt
中的内容:
- Divided focus leads to divided results.
- Where I point my attention becomes how I spend my life.
- What is natural is not supernatural.
- Divsasdided focus leads to divided results.
- Where I point my attadasdntion becomes how I spend my life.
- What is natural is dasdanot supernatural.
- Diviadsadded focus leaasadaads to divided results.
- Where I point myadasd attention becomes hasdaow I spend my life.
- What is naturaladasd is not supernatural.
- Dividedassassss focus leads to dividesssd results.
- Whersssse I point my attention becomes hosssssw I spend my life.
- Whdsdasfasfaat is natural is not dfgsdfsdfsdsupernatural.
在从 here 启动的 Jupyter 环境中使用该代码和文本文件内容进行尝试,以分离出您首先要进行的机器或特定文件。
append 语句中的切片删除了行尾。如果您涉及奇怪的行结尾,这可能对您有用。如果你有典型的行尾,你可以只使用 strip()
方法,即,为了附加使用 lines.append(line.strip())
.
通过将行尾添加回最后 join
,您可以避免处理可能合并的最后一行,因为它没有行尾而其他行有。