仅将特定行从文件 a 写入文件 b
write only specific lines from file a to file b
我在只将感兴趣的行写入新文件时遇到问题。
f1文件由raws组成,内容如下:
line =
'Raw,512561008,58913000000,,,-1226439751087359050,0.0,6.748478024207768,,,0,14,0.0,16397,510195928489024,10,33.76073455810547,91.65148631320972,0.0037611895240843296,1,6907.217495186118,0.0018805947620421648,1.56109798E9,,,,0,,5,,1.56109798E9'
我有兴趣编写索引 [-15:-14] 之间不包含“5”的行。我已经编写了以下函数,但是当我 运行 它时,新文件仍然包含不需要的行。如果有人能指出我做错了什么,我将不胜感激。谢谢!
def CleanGNSSLogger(ASCIIfileName,filterStr=None):
try:
with open(ASCIIfileName, 'r') as f1, open(ASCIIfileName[:-4]+'_clean'+ASCIIfileName[-4:],'w') as f2:
for line in f1:
if line[-15:-14] != '5':
f2.write(line)
except IOError as e:
print ('Operation failed: %s' % e.strerror)
return
尝试
if "5" not in line[-15:-14]:
看看这是否有效。
我会做这样的事情,所以你要确保你感兴趣的值 (5) 是可访问的:
SOME_NAME_INDEX = 29 # or the correct location of the value of interest
def CleanGNSSLogger(ASCIIfileName,filterStr=None):
try:
with open(ASCIIfileName, 'r') as f1, open(ASCIIfileName[:-4]+'_clean'+ASCIIfileName[-4:],'w') as f2:
for line in f1:
values = line.split(",")
if values[SOME_NAME_INDEX] != '5':
f2.write(line)
except IOError as e:
print ('Operation failed: %s' % e.strerror)
我在只将感兴趣的行写入新文件时遇到问题。
f1文件由raws组成,内容如下:
line =
'Raw,512561008,58913000000,,,-1226439751087359050,0.0,6.748478024207768,,,0,14,0.0,16397,510195928489024,10,33.76073455810547,91.65148631320972,0.0037611895240843296,1,6907.217495186118,0.0018805947620421648,1.56109798E9,,,,0,,5,,1.56109798E9'
我有兴趣编写索引 [-15:-14] 之间不包含“5”的行。我已经编写了以下函数,但是当我 运行 它时,新文件仍然包含不需要的行。如果有人能指出我做错了什么,我将不胜感激。谢谢!
def CleanGNSSLogger(ASCIIfileName,filterStr=None):
try:
with open(ASCIIfileName, 'r') as f1, open(ASCIIfileName[:-4]+'_clean'+ASCIIfileName[-4:],'w') as f2:
for line in f1:
if line[-15:-14] != '5':
f2.write(line)
except IOError as e:
print ('Operation failed: %s' % e.strerror)
return
尝试
if "5" not in line[-15:-14]:
看看这是否有效。
我会做这样的事情,所以你要确保你感兴趣的值 (5) 是可访问的:
SOME_NAME_INDEX = 29 # or the correct location of the value of interest
def CleanGNSSLogger(ASCIIfileName,filterStr=None):
try:
with open(ASCIIfileName, 'r') as f1, open(ASCIIfileName[:-4]+'_clean'+ASCIIfileName[-4:],'w') as f2:
for line in f1:
values = line.split(",")
if values[SOME_NAME_INDEX] != '5':
f2.write(line)
except IOError as e:
print ('Operation failed: %s' % e.strerror)