Python 根据Ist列对文件进行排序,同时修改文件中的第二列
在这个文件中,我有两列,其中Ist列描述浮点时间,第二列表示字符串Python 根据Ist列对文件进行排序,同时修改文件中的第二列,python,Python,在这个文件中,我有两列,其中Ist列描述浮点时间,第二列表示字符串 <time> <str> 8.45 A 6.12 B 10.12 C 1.45 D 2.12 E 0.45 F .... ... .. 请帮忙!我解决这个问题的方法是:我把每一列分别放在两个列表中。我用浮点数对数组进行排序,同时进行必要的调整。我没有看到“同时修改第二列”部分。 with open('filename.txt', 'w') as fout: for line in sorted
<time> <str>
8.45 A
6.12 B
10.12 C
1.45 D
2.12 E
0.45 F
....
...
..
请帮忙!我解决这个问题的方法是:我把每一列分别放在两个列表中。我用浮点数对数组进行排序,同时进行必要的调整。我没有看到“同时修改第二列”部分。
with open('filename.txt', 'w') as fout:
for line in sorted(open('filename.txt', 'r').readlines(), key=lambda x:float(x.split()[0])):
fout.write(line)
with open('filename.txt', 'w') as fout:
for line in sorted(open('filename.txt', 'r').readlines(), key=lambda x:float(x.split()[0])):
fout.write(line)
li = ['Do you know how to read a file ?',
'Do you know how to split a string into a list ?',
'Do you know how to sort a list ?',
'Do you know how to join string elements of a list ?',
'Do you know how to write in a file ?']
def your_answer(e):
y = 'go'
while y not in ('yes','no'):
y = raw_input('%s\nenter yes or no :' % e)
return y
if any(your_answer(e)=='no' for e in li):
print 'Please, read the documentations.'
else:
print 'Show what you tried.'