Python 解压文件时,先删除不需要的行,然后再将其写入文件
我在尝试解压缩文件时出错,删除我不感兴趣的行,最后将剩余的行写入文件。这是我的密码:Python 解压文件时,先删除不需要的行,然后再将其写入文件,python,python-3.x,gzip,Python,Python 3.x,Gzip,我在尝试解压缩文件时出错,删除我不感兴趣的行,最后将剩余的行写入文件。这是我的密码: import gzip, os, sys dataset_names=[] dir_path=('local drive path') dataset_names= os.listdir(dir_path) count=0 read_zip = []; for dataset in dataset_names: each_dataset=os.path.join(dir_path+'\\'+da
import gzip, os, sys
dataset_names=[]
dir_path=('local drive path')
dataset_names= os.listdir(dir_path)
count=0
read_zip = [];
for dataset in dataset_names:
each_dataset=os.path.join(dir_path+'\\'+dataset+'\\'+'soft'+'\\'+dataset+'_full'+'.soft')
with gzip.open(each_dataset+'.gz', 'rb') as each_gzip_file:
if count == 2: # I wanted to check with 2 datasets first
continue;
for line in each_gzip_file:
if line.startwith !=('#', '!', '^'):
continue;
read_zip.append('\t' + line);
with open('name of a file', 'wb') as f:
f.writelines(read_zip)
print(dataset);
count+=1;
以下是我得到的错误:
AttributeError: 'bytes' object has no attribute 'startwith'
然后我尝试将其更改为以下代码:
......
.......
for line in each_gzip_file:
if not PY3K:
if lines.startwith != ('#', '!', '^'):
continue;
lines.append(line)
else:
lines.append(line.decode('cp437'))
makeitastring = ''.join(map(str, lines))
with open('fine name', 'wb') as f:
my_str_as_bytes = str.encode(str(,lines))
f.writelines(makeitastring)
这一次出现了以下错误:
TypeError: a bytes-like object is required, not 'str'
我还用以下内容对其进行了更改,但它也不起作用。这就像是一次又一次的迭代:
for line in each_gzip_file:
read_zip.append(line);
for x in read_zip:
if str(x).startswith != ('#', '!', '^'):
continue;
else:
final.append(x);
with open('file name', 'ab') as f:
f.writelines(final)
我遗漏了什么吗?
谢谢,我看到两个错误。首先,您拼错了方法名称。它是
bytes.startwith()
,而不是bytes.startwith()
。注意“开始”和“带”之间的“s”
其次,代码line.startswith!=(“#”、“!”和“^”)
没有按照您的想法行事startswith()
是一个包含字节
对象的方法,您需要使用“#”
等作为参数来调用该方法。现在,您会问“这个方法是否等于这个由三个字符串组成的元组?”。在这种情况下,这是没有意义的,但是Python将愉快地返回False
您需要的是
line.startswith((b'#',b'!',b'^')
。(区分字符串和字节需要b
,因为它们在Python 3中是不同的。)如果行以这三个字符中的任何一个开头,这将返回True
。您能指定哪一行实际触发错误吗?@Saustin for line for line in each_gzip_文件:if line.startwith=(“#”、“!”和“^”):继续;你试过了吗代码>?@VBB我做了。这就是那个错误:if str(line).startwith=(“#”、“!”、“^”):AttributeError:“str”对象没有属性“startwith”谢谢@bnacher,但我仍然得到一个错误:if line.startswith(b'#',b'!',b'^'):TypeError:切片索引必须是整数或无,或具有索引方法字节。startswith
将字节对象或字节对象元组作为第一个参数,但是你把它们作为第一、第二和第三个参数传递,因为你去掉了括号。第二个和第三个参数应该是整数,这将导致出现错误消息。再次阅读bnaecker的代码,它在那里是正确的。@bnaecker谢谢!你是对的,我被一对括号打断了!