简体   繁体   English

readlines 从遍历数组和 writelines 到新文件

[英]readlines from iterating through an array and writelines to a new file

bad_list is an array that is returned from a different function, returns the line numbers of rows that are problematic and need to be looked at more closely bad_list 是一个从不同函数返回的数组,返回有问题需要更仔细查看的行的行号

eg array([ 1, 3, 4, 27, 50, 99], dtype=int64)例如array([ 1, 3, 4, 27, 50, 99], dtype=int64)


The idea is to read test.txt, and make a new test_badlines.txt which only contains the problematic lines as specified in bad_list这个想法是读取 test.txt,并创建一个新的 test_badlines.txt,其中只包含 bad_list 中指定的有问题的行


Here is what I have so far, the print line works but the writelines only spits out 1 line when it should be 6 lines这是我到目前为止所拥有的,打印行有效,但写行只在应该是 6 行时吐出 1 行

for rows in bad_list:
    filename = 'C:\\Users\\Username\\Downloads\\test.txt'
    bad_filename = str(filename)[:-4] + '_badlines.txt'
    with open(filename) as f,  open(bad_filename, 'w') as wt: 
        lines = f.readlines()
        #print lines[rows]
        wt.writelines(lines[rows])

lines is a plain list , and plain list s can't be indexed by a sequence of indices to look up. lines是一个普通的list ,并且普通的list不能通过要查找的索引序列进行索引。 The simplest solution here is to make a generator expression that pulls out the lines you're concerned with, replacing:这里最简单的解决方案是制作一个生成器表达式来提取您关心的行,替换:

wt.writelines(lines[rows])

with:和:

wt.writelines(lines[row] for row in bad_list)

Assuming bad_list is the array you described, you drop the outer for loop entirely;假设bad_list是您所描述的数组,则完全删除外部for循环; you don't want to open the input and output files over and over once per row, just once total.您不想每行一遍又一遍地打开输入和输出文件,总共只打开一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM