Compatibility with both Python 2(.7) and 3
This commit is contained in:
@@ -2,18 +2,18 @@ import sys
|
||||
|
||||
def save_data(group_data,output_feature,output_group):
|
||||
if len(group_data) == 0:
|
||||
return
|
||||
return
|
||||
|
||||
output_group.write(str(len(group_data))+"\n")
|
||||
for data in group_data:
|
||||
# only include nonzero features
|
||||
feats = [ p for p in data[2:] if float(p.split(':')[1]) != 0.0 ]
|
||||
output_feature.write(data[0] + " " + " ".join(feats) + "\n")
|
||||
output_feature.write(data[0] + " " + " ".join(feats) + "\n")
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 4:
|
||||
print "Usage: python trans_data.py [Ranksvm Format Input] [Output Feature File] [Output Group File]"
|
||||
sys.exit(0)
|
||||
print ("Usage: python trans_data.py [Ranksvm Format Input] [Output Feature File] [Output Group File]")
|
||||
sys.exit(0)
|
||||
|
||||
fi = open(sys.argv[1])
|
||||
output_feature = open(sys.argv[2],"w")
|
||||
@@ -22,16 +22,16 @@ if __name__ == "__main__":
|
||||
group_data = []
|
||||
group = ""
|
||||
for line in fi:
|
||||
if not line:
|
||||
break
|
||||
if "#" in line:
|
||||
line = line[:line.index("#")]
|
||||
if not line:
|
||||
break
|
||||
if "#" in line:
|
||||
line = line[:line.index("#")]
|
||||
splits = line.strip().split(" ")
|
||||
if splits[1] != group:
|
||||
save_data(group_data,output_feature,output_group)
|
||||
group_data = []
|
||||
group = splits[1]
|
||||
group_data.append(splits)
|
||||
if splits[1] != group:
|
||||
save_data(group_data,output_feature,output_group)
|
||||
group_data = []
|
||||
group = splits[1]
|
||||
group_data.append(splits)
|
||||
|
||||
save_data(group_data,output_feature,output_group)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user