如何选择数据行
回答
使用csv
module读写,那么就过滤:
with open(inputfilename, 'rb') as inputfile, open(outputfile, 'wb') as outputfile:
reader = csv.reader(inputfile)
writer = csv.writer(outputfile)
for row in reader:
if minval <= int(row[2]) <= maxval:
writer.writerow(row)
这会很好,但他的数据格式不可能与csv模块一起工作。 – 2013-04-27 12:24:34
可能性在那里,但考虑到OP知道它是整个文件中的4列,它很可能会工作。答案很容易根据大多数格式进行调整,否则一个快速的'.split(..)'也许有一些剥离会为其余的99%做好准备。 – 2013-04-27 12:26:04
这是真的。您也可能需要添加一个分隔符,具体取决于csv的格式。我想我们只能等待一些更多的信息。 – 2013-04-27 12:27:36
可以用简单的CSV读/写来完成。 可以更优雅地使用Numpy以矢量化的形式完成,而且由于行数很大,Numpy可能会快得多。
import numpy as np
#Load file into a 'MATRIX'
data=np.loadtxt('name_of_delimited_file.txt')
# Find indices where the condition is met
idx_condition_met=(data[:,2] > min) & (data[:,2] < max)
np.savetxt('output.txt', data[idx_condition_met], delimiter=',')
- 1. 如何通过行名选择数据
- 2. 如何选择由数行
- 3. 如何选择数据-ID和数据在行动jQuery的
- 4. 如何从R中的数据帧中选择12行数据
- 5. jquery数据表行选择
- 6. Primefaces数据表行选择
- 7. 数据表多行选择
- 8. JSF数据表行选择
- 9. 如何根据另一个选择查询的数据运行选择查询
- 10. 如何根据用户名从数据库中选择行?
- 11. 如何根据两列值选择行?
- 12. 如何根据按钮选择行?
- 13. 如何根据行值选择列
- 14. 如何根据选择值从数组中选择选项?
- 15. 如何在数组中选择数据库下一行?
- 16. 如何从数据库中选择倒数第二行?
- 17. 如何在选择中进行选择
- 18. 选择数据
- 19. 选择数据
- 20. 选择数据
- 21. 选择数据
- 22. 选择数据
- 23. 选择数据
- 24. 选择数据
- 25. 选择数据
- 26. 80行数据,第一行选择1-20行,第二行选择21-30行
- 27. 如何使用jquery选择和取消选择数据?
- 28. 如何从MySql中的选择查询中选择数据?
- 29. 如何选择我的SQLite数据库
- 30. Primefaces如何选择数据表事件
你可以让Python执行一条SQL语句,将这行插入到另一个表中。阅读Python如何与SQL数据库进行通信。你的实际条件标准并不是很清楚地表达出来的。 – Tim 2013-04-27 12:20:01
到目前为止您尝试过什么?该文件的格式是否适合'csv'模块? – 2013-04-27 12:20:08
@Tim:如果需要写出新文件,则不需要使用SQL。您需要阅读整个文件*反正*,因此您可以逐行过滤并写出匹配的行。 – 2013-04-27 12:20:28