我试图从bz2文件中提取10'000个第一行.
import bz2
file = "file.bz2"
file_10000 = "file.txt"
output_file = codecs.open(file_10000,'w+','utf-8')
source_file = bz2.open(file, "r")
count = 0
for line in source_file:
count += 1
if count < 10000:
output_file.writerow(line)
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误"'模块'对象没有属性'打开'".你有什么想法?或者可能是我可以用其他方式将10'000个第一行保存到txt文件中?我在Windows上.
我必须从第 2 列中获取值的总和,但我应该每个 id 只计算一次。例如,对于下表,我需要 5 + 10 + 7 而不是 5 + 5 + 10 + 7
id column1 column2
1 value1 5
1 value1 5
2 value1 10
3 value1 7
Run Code Online (Sandbox Code Playgroud)
后者我通过以下查询得到,但我不知道如何调整它以获得第一个总和。我应该像选择所有不同的行一样预先过滤表吗?
SELECT column1, sum(column2) FROM table group by column1;
Run Code Online (Sandbox Code Playgroud)