如何打印由数字列表 Python 指定的文件行?
how can I print lines of a file that specefied by a list of numbers Python?
我打开字典并提取特定的行,这些行将使用列表指定,最后我需要在一行中打印一个完整的句子。
我想打开字典每行一个词
然后在一行中打印一个句子,在单词之间加上 space:
N = ['19','85','45','14']
file = open("DICTIONARY", "r")
my_sentence = #?????????
print my_sentence
如果您的 DICTIONARY
不是太大(即可以适合您的记忆):
N = [19,85,45,14]
with open("DICTIONARY", "r") as f:
words = f.readlines()
my_sentence = " ".join([words[i].strip() for i in N])
编辑: 一个小的说明,原来的post没有用space来加入的话,我已经把代码改成包含它。如果您需要用逗号或您可能需要的任何其他分隔符分隔单词,也可以使用 ",".join(...)
。另外,请记住,此代码使用 zero-based 行索引,因此 DICTIONARY
的第一行将为 0,第二行将为 1,依此类推
更新::如果你的字典对你的记忆来说太大了,或者你只是想消耗尽可能少的内存(如果是这样的话,你为什么会去Python 首先?;)) 你只能 'extract' 你感兴趣的词:
N = [19, 85, 45, 14]
words = {}
word_indexes = set(N)
counter = 0
with open("DICTIONARY", "r") as f:
for line in f:
if counter in word_indexes:
words[counter] = line.strip()
counter += 1
my_sentence = " ".join([words[i] for i in N])
您可以使用 linecache.getline
获取您想要的特定行号:
import linecache
sentence = []
for line_number in N:
word = linecache.getline('DICTIONARY',line_number)
sentence.append(word.strip('\n'))
sentence = " ".join(sentence)
这是一个更基本的简单方法:
n = ['2','4','7','11']
file = open("DICTIONARY")
counter = 1 # 1 if you're gonna count lines in DICTIONARY
# from 1, else 0 is used
output = ""
for line in file:
line = line.rstrip() # rstrip() method to delete \n character,
# if not used, print ends with every
# word from a new line
if str(counter) in n:
output += line + " "
counter += 1
print output[:-1] # slicing is used for a white space deletion
# after last word in string (optional)
我打开字典并提取特定的行,这些行将使用列表指定,最后我需要在一行中打印一个完整的句子。
我想打开字典每行一个词 然后在一行中打印一个句子,在单词之间加上 space:
N = ['19','85','45','14']
file = open("DICTIONARY", "r")
my_sentence = #?????????
print my_sentence
如果您的 DICTIONARY
不是太大(即可以适合您的记忆):
N = [19,85,45,14]
with open("DICTIONARY", "r") as f:
words = f.readlines()
my_sentence = " ".join([words[i].strip() for i in N])
编辑: 一个小的说明,原来的post没有用space来加入的话,我已经把代码改成包含它。如果您需要用逗号或您可能需要的任何其他分隔符分隔单词,也可以使用 ",".join(...)
。另外,请记住,此代码使用 zero-based 行索引,因此 DICTIONARY
的第一行将为 0,第二行将为 1,依此类推
更新::如果你的字典对你的记忆来说太大了,或者你只是想消耗尽可能少的内存(如果是这样的话,你为什么会去Python 首先?;)) 你只能 'extract' 你感兴趣的词:
N = [19, 85, 45, 14]
words = {}
word_indexes = set(N)
counter = 0
with open("DICTIONARY", "r") as f:
for line in f:
if counter in word_indexes:
words[counter] = line.strip()
counter += 1
my_sentence = " ".join([words[i] for i in N])
您可以使用 linecache.getline
获取您想要的特定行号:
import linecache
sentence = []
for line_number in N:
word = linecache.getline('DICTIONARY',line_number)
sentence.append(word.strip('\n'))
sentence = " ".join(sentence)
这是一个更基本的简单方法:
n = ['2','4','7','11']
file = open("DICTIONARY")
counter = 1 # 1 if you're gonna count lines in DICTIONARY
# from 1, else 0 is used
output = ""
for line in file:
line = line.rstrip() # rstrip() method to delete \n character,
# if not used, print ends with every
# word from a new line
if str(counter) in n:
output += line + " "
counter += 1
print output[:-1] # slicing is used for a white space deletion
# after last word in string (optional)