我的CSV文件是这样的。
0.0063,0.0121,band -> mcr music
0.0061,0.0123,band -> mcr
0.0062,0.0122,band -> orchestra
如何对csv文件的第一列进行排序并打印每一行?因此,在这种情况下,最终输出应为
0.0061,0.0123,band -> mcr
0.0062,0.0122,band -> orchestra
0.0063,0.0121,band -> mcr music
答案 0 :(得分:1)
csv基本上是一个数组(矩阵)的python数组。那说你的数据实际上如下:
csv = [
[0.0063, 0.0121, 'band -> mcr music'],
[0.0061, 0.0123, 'band -> mcr'],
[0.0062, 0.0122, 'band -> orchestra']
]
然后您可以考虑从 ith 列排序为sorting a list of tuples。你会这样做:
csv = sorted(csv, key=lambda x: x[0])
或者,您可以使用数组内置sort
方法进行排序:
csv.sort(key=lambda x:x[0])
现在要打印每一行,你可以迭代数组:
for line in csv:
print(line)
要按照原始问题(由,
分隔的值)获得输出:
print(','.join(line))
答案 1 :(得分:0)
这是大熊猫中的等价物。如果您想更快地访问该文件,可以查看以下内容:http://pythondata.com/working-large-csv-files-python/。该指南将帮助您建立csv的数据库。
import pandas as pd
data = '''\
0.0063,0.0121,band -> mcr music
0.0061,0.0123,band -> mcr
0.0062,0.0122,band -> orchestra'''
file = pd.compat.StringIO(data) # Replace with path/to/file
df = pd.read_csv(file, sep=',', header=None).sort_values(by=1, ascending=False)
for i in df.values:
print(i)
#df.to_csv('path/to/outfile', index=False, header=False)
打印:
[0.0061 0.0123 'band -> mcr']
[0.0062 0.0122 'band -> orchestra']
[0.0063 0.0121 'band -> mcr music']