[英]Reading from the first line in a multiline string
I have a directory with files as:我有一个包含文件的目录:
ab_list
bd_list
cd_list
mno_list
hk_list
pd_list
I have another file called testfile as outside this directory as:我在此目录之外还有另一个名为 testfile 的文件:
abc
que nw
ab_list ON 8
gs_list ON 9
hk_list OFF 9
bd_list ON 7
cd_list OFF 6
fr_list ON 5
mno_list ON 4
pq_list OFF 6
jk_list ON 7
pd_list OFF 8
I want to compare the 2 and all the files with filename and ON next to it(if matched) their content should get merged into a new file called merge_file.我想比较 2 和所有带有文件名和 ON 的文件(如果匹配),它们的内容应该合并到一个名为 merge_file 的新文件中。 The other files which match with testfile but have OFF, their filenames should be printed in a new_file.
与 testfile 匹配但为 OFF 的其他文件,其文件名应打印在 new_file 中。 contents of ab_list bd_list and mno_list should get merged into top_file
ab_list bd_list 和 mno_list 的内容应该合并到 top_file
here is the code I have tried till now:这是我到目前为止尝试过的代码:
from glob import glob
test_file_directory = "C:\\Users\\User\\Desktop\\Folder\\"
files1 = glob("*.txt")
with open(test_file_directory+"testfile.txt","r") as f:
files2 = [' '.join([l.split()[0],l.split()[1]]) for l in f.readlines()[3:]]
for f1 in files1:
for f2 in files2:
if f1[:-4]+' ON' == f2:
#print('match')
with open('merge_file.txt','a') as a:
with open(f1,'r') as r:
a.write(r.readlines()[1:]+'\n')
elif f1[:-4]+' OFF' == f2:
#print('match')
with open('match_file.txt','a') as a:
with open(f1,'r') as r:
a.write(f"{f2} {len(r.readlines())}\n")
elif f1[:-4]+' ON' == f2:
#print('match')
with open('match_file.txt','a') as a:
with open(f1,'r') as r:
a.write(f"{f2} {len(r.readlines())}\n")
I want that the file contents being written in merge_file start being read from second line of file instead of first and match_file also has the filenames with ON next to it(it has OFF from the first elif) a.write(r.readlines()[1:]+'\n'
this line gives an error saying it is a string and not a list.我希望写入merge_file的文件内容开始从文件的第二行而不是第一行读取,并且match_file的文件名旁边也有ON(第一个elif为OFF)
a.write(r.readlines()[1:]+'\n'
这一行给出一个错误,指出它是一个字符串而不是一个列表。
a.write(r.readlines()[1:]+'\n')
a.write(r.readlines()[1:]+'\n')
It perform an add with r.readlines()[1:] and '\n'.它使用 r.readlines()[1:] 和 '\n' 执行添加。
r.readlines()[1:] is a list with each line in file. r.readlines()[1:] 是文件中每一行的列表。
So you got error所以你有错误
TypeError: can only concatenate list (not "str") to list
TypeError:只能将列表(不是“str”)连接到列表
What you should do is to join all line in list with '\n' by using join, like您应该做的是使用连接将列表中的所有行与 '\n' 连接起来,例如
a.write('\n'.join(r.readlines()[1:])+'\n')
Anyway, to copy content of file, you should use f.read(), not f.readlines()无论如何,要复制文件的内容,您应该使用 f.read(),而不是 f.readlines()
a.write(r.read()+'\n')
To get ride first line, you can use str.partition('\n'), it divide source string to three partitions as [first_string, '\n', next_string], like要获取第一行,您可以使用 str.partition('\n'),它将源字符串划分为三个分区,如 [first_string, '\n', next_string],如
a.write(r.read().partition('\n')[2]+'\n')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.