我是python的新手。试图编写一个脚本,该脚本将使用来自文件的数字变体,其中还包含一个标题。这里是一个文件的例子:如何使用python跳过多个标题行
@File_Version: 4
PROJECTED_COORDINATE_SYSTEM
#File_Version____________-> 4
#Master_Project_______->
#Coordinate_type_________-> 1
#Horizon_name____________->
sb+
#Horizon_attribute_______-> STRUCTURE
474457.83994 6761013.11978
474482.83750 6761012.77069
474507.83506 6761012.42160
474532.83262 6761012.07251
474557.83018 6761011.72342
474582.82774 6761011.37433
474607.82530 6761011.02524
我想跳过标题。这是我的尝试。当然,如果我知道哪些字符会出现在标题中,如“#”和“@”,它是有效的。但是,我怎样才能跳过包含任何字母字符的所有行?
in_file1 = open(input_file1_short, 'r')
out_file1 = open(output_file1_short,"w")
lines = in_file1.readlines()
x = []
y = []
for line in lines:
if "#" not in line and "@" not in line:
strip_line = line.strip()
replace_split = re.split(r'[ ,|;"\t]+', strip_line)
x = (replace_split[0])
y = (replace_split[1])
out_file1.write("%s\t%s\n" % (str(x),str(y)))
in_file1.close()
非常感谢!
你可以简单地检查前导字符,还是比你的头部检测更普遍?如果你可以在前面有数字,但后来得到的话,那么也许我可以给你写一个简化函数。 – Prune