从.txt或.csv Python中提取前2行和最后一行
我需要从大量的.txt和.csv文件中提取前2行和最后一行。如何允许用户选择一个文件并输出一个新的.txt或.csv文件,其中只包含这3行?这就是您需要的:从.txt或.csv Python中提取前2行和最后一行,python,csv,extract,Python,Csv,Extract,我需要从大量的.txt和.csv文件中提取前2行和最后一行。如何允许用户选择一个文件并输出一个新的.txt或.csv文件,其中只包含这3行?这就是您需要的: def extract_lines(filename,outputname): l = [] with open(filename,'r') as f: for index,line in enumerate(f): #This iterates the file line by line which is
def extract_lines(filename,outputname):
l = []
with open(filename,'r') as f:
for index,line in enumerate(f): #This iterates the file line by line which is memory efficient in case the csv is huge.
if index < 2: #first 2 lines
l.append(line)
if index > 1: # means the file has at least 3 lines
l.append(line)
with open(outputname,'w') as f:
for line in l:
f.write(line)
def extract_行(文件名、输出名):
l=[]
将open(filename,'r')作为f:
对于索引,enumerate(f)中的行:#这会逐行迭代文件,在csv很大的情况下,这会节省内存。
如果索引<2:#前两行
l、 追加(行)
如果索引>1:#表示文件至少有3行
l、 追加(行)
将open(outputname,'w')作为f:
对于l中的行:
f、 写(行)
这就是您需要的:
def extract_lines(filename,outputname):
l = []
with open(filename,'r') as f:
for index,line in enumerate(f): #This iterates the file line by line which is memory efficient in case the csv is huge.
if index < 2: #first 2 lines
l.append(line)
if index > 1: # means the file has at least 3 lines
l.append(line)
with open(outputname,'w') as f:
for line in l:
f.write(line)
def extract_行(文件名、输出名):
l=[]
将open(filename,'r')作为f:
对于索引,enumerate(f)中的行:#这会逐行迭代文件,在csv很大的情况下,这会节省内存。
如果索引<2:#前两行
l、 追加(行)
如果索引>1:#表示文件至少有3行
l、 追加(行)
将open(outputname,'w')作为f:
对于l中的行:
f、 写(行)
我认为您也可以使用bash脚本来实现这一要求
#!/bin/bash
for file in $(find . -name '*.txt' -o -name '*.csv' )
do
sed -n -e '1,2p' -e '$p' ${file} > "result"${file:(-5)}
done
此脚本将搜索以txt或csv结尾的所有文件。它将剪切前两行和最后一行,将这些行存储在一个新文件中
例如,我有三个名为file1.txt、file2.txt、file3.csv的文件,它将为每个文件剪切三行,并分别存储在result1.txt、result2.txt、result3.csv中。我认为您也可以使用bash脚本来实现此要求
#!/bin/bash
for file in $(find . -name '*.txt' -o -name '*.csv' )
do
sed -n -e '1,2p' -e '$p' ${file} > "result"${file:(-5)}
done
def get_lines(filename, front=2, rear=1):
result = []
with open(filename, 'rb') as f:
for i, val in enumerate(f):
if i >= front:
break
result.append(val)
back_pos = -2
f.seek(back_pos, 2) # jump to the second end byte
rear_count = 0
while True:
if '\n' in f.read(1):
rear_count += 1
if rear_count >= rear:
result.extend(f.readlines())
break
back_pos -= 1
f.seek(back_pos, 2)
return result
此脚本将搜索以txt或csv结尾的所有文件。它将剪切前两行和最后一行,将这些行存储在一个新文件中
例如,我有三个名为file1.txt、file2.txt、file3.csv的文件,它将为每个文件剪切三行,并将它们分别存储在result1.txt、result2.txt、result3.csv中
def get_lines(filename, front=2, rear=1):
result = []
with open(filename, 'rb') as f:
for i, val in enumerate(f):
if i >= front:
break
result.append(val)
back_pos = -2
f.seek(back_pos, 2) # jump to the second end byte
rear_count = 0
while True:
if '\n' in f.read(1):
rear_count += 1
if rear_count >= rear:
result.extend(f.readlines())
break
back_pos -= 1
f.seek(back_pos, 2)
return result
第一行容易读,但最后一行难读。
到国际热核聚变实验堆的速度非常慢
第一行容易读,但最后一行难读。
到iter行的速度非常慢。这样,您可以返回您想要的行,这是使用范围的唯一问题
df=open(r"D:\...\nameFile.txt",encoding='utf8')
def etiqueta(df):
lista=[]
for line,x in zip(df,range(0,2)):
lista.append(line)
return lista
etiqueta(df)
通过这种方式,您可以返回您想要的线,这是使用该范围的唯一问题
df=open(r"D:\...\nameFile.txt",encoding='utf8')
def etiqueta(df):
lista=[]
for line,x in zip(df,range(0,2)):
lista.append(line)
return lista
etiqueta(df)
@再见。它应该在循环之外。If语句检查文件中的行数是否至少为3行。如果文件只有两行,那么就没有必要添加“last”行,因为前两行包含“last”。True,my bad。实际上,我会通过读取循环的前两行并立即丢弃循环来提高效率。这样就不需要对每一行计算
if
,这对于大文件可能需要一些时间。@adrio,True。如果问题提到了最快的方法,我可能会使用seek
。@adrionope。它应该在循环之外。If语句检查文件中的行数是否至少为3行。如果文件只有两行,那么就没有必要添加“last”行,因为前两行包含“last”。True,my bad。实际上,我会通过读取循环的前两行并立即丢弃循环来提高效率。这样就不需要对每一行计算if
,这对于大文件可能需要一些时间。@adrio,True。如果问题提到最快的方法,我可能会使用seek
。