[英]Sorting a list from a file, outputting in another file
I am trying to find the min and max out of a csv file, and have it output into a text file, currently my code outputs all data into the output file, and I am unsure of how to grab the data out of the multiple columns and have them sorted accordingly. 我正在尝试从csv文件中找到最小值和最大值,并将其输出到文本文件中,当前我的代码将所有数据输出到输出文件中,并且我不确定如何从多列中获取数据并对其进行相应的排序。
Any guidance would be appreciated, as I don't have a good lead on how to figure this out 任何指导将不胜感激,因为我对如何解决这个问题没有很好的领导
read_file = open("riskfactors.csv", 'r')
def create_file():
read_file = open("riskfactors.csv", 'r')
write_file = open("best_and_worst.txt", "w")
for line_str in read_file:
read_file.readline()
print (line_str,file=write_file)
write_file.close()
read_file.close()
Assuming your file is a standard .csv file containing only numbers separated by semicolons: 假设您的文件是标准的.csv文件,其中仅包含用分号分隔的数字:
1;5;7;6;
3;8;1;1;
Then it's easiest to use the str.split() command, followed by a type conversion to int. 然后,最简单的方法是使用str.split()命令,然后将类型转换为int。 You could store all values in a list (or quicker: set) and then get the maximum:
您可以将所有值存储在列表中(或更快速地设置),然后获取最大值:
valuelist=[]
for line_str in read_file:
for cell in line_str.split(";"):
valuelist.append(int(cell))
print(max(valuelist))
print(min(valuelist))
Warning: If your file contains non-number entries you'd have to filter them out. 警告:如果文件中包含非数字条目,则必须将其过滤掉。 .csv-files can also have different delimiters.
.csv文件也可以具有不同的定界符。
import sys, csv
def cmp_risks(x, y):
# This assumes risk factors are prioritised by key columns 1, 3
# and that column 1 is numeric while column 3 is textual
return cmp(int(x[0]), int(y[0])) or cmp(x[2], y[2])
l = sorted(csv.reader(sys.stdin), cmp_risks))
# Write out the first and last rows
csv.writer(sys.stdout).writerows([l[0], l[len(l)-1]])
Now, I took a shortcut and said the input and output files were sys.stdin
and sys.stdout
. 现在,我走了一条捷径,说输入和输出文件是
sys.stdin
和sys.stdout
。 You'd probably replace these with the file objects you created in your original question. 您可能将它们替换为在原始问题中创建的文件对象。 (eg
read_file
and write_file
) (例如
read_file
和write_file
)
However, in my case, I'd probably just run it (if I were using linux) with: 但是,就我而言,我可能只是使用以下命令运行它(如果使用的是Linux):
$ ./foo.py <riskfactors.csv >best_and_worst.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.