Q
CSV讀取特定行
5
A
回答
6
你可以使用一個list comprehension
到文件中篩選像這樣:
with open('file.csv') as fd:
reader=csv.reader(fd)
interestingrows=[row for idx, row in enumerate(reader) if idx in (28,62)]
# now interestingrows contains the 28th and the 62th row after the header
1
您簡單地跳過行的必要數量:
with open("test.csv", "rb") as infile:
r = csv.reader(infile):
for i in range(8): # count from 0 to 7
next(r) # and discard the rows
row = next(r) # "row" contains row number 9 now
2
你可以閱讀所有這些,然後使用普通的列表來找到它們。
with open('bigfile.csv','rb') as longishfile:
reader=csv.reader(longishfile)
rows=[r for r in reader]
print row[9]
print row[88]
如果你有一個巨大的文件,這會殺了你的記憶,但如果該文件的少得了1個萬多行,你不應該遇到任何大的減速。
+2
你可以簡單的'rows = list(reader)' – 2014-10-20 11:37:19
0
使用list
抓住所有的行同時作爲一個列表。然後通過列表中的索引/偏移訪問您的目標行。例如:
#!/usr/bin/env python
import csv
with open('source.csv') as csv_file:
csv_reader = csv.reader(csv_file)
rows = list(csv_reader)
print(rows[8])
print(rows[22])
相關問題
- 1. 從特定行讀取csv
- 2. PHP-從特定行數讀取csv行?
- 3. 在Python中讀取特定的CSV行
- 4. CSVProvider開始讀取特定行的csv
- 5. 如何從csv讀取特定列?
- 6. 閱讀csv中的特定行
- 7. 從特定行讀取特定數字
- 8. StreamReader讀取特定行號
- 9. read.csv讀取特定行
- 10. PHP讀取特定行
- 11. Office.js讀取excel特定行
- 12. 閱讀csv的特定列
- 13. 如何讀取/打印特定的列和行蟒蛇CSV
- 14. 如何在Python中讀取CSV文件中的特定行
- 15. 如何讀取WPF中的.csv文件中的特定行(xaml)
- 16. 從excel csv文件中讀取特定的行/列
- 17. 從許多csv文件中讀取特定行,python
- 18. 讀取特定的CSV行並在輸出框中顯示
- 19. 讀取CSV文件中的特定行,蟒蛇
- 20. 在PHP中讀取csv文件上的特定行
- 21. Python使用列表理解從CSV讀取特定行
- 22. 在python中讀取.csv的特定行數
- 23. opencsv CSV Reader - 從特定行開始讀取。
- 24. Python Pandas使用特定行終止符讀取CSV文件
- 25. Python讀取字典後,CSV CSV搜索特定值
- 26. 找到特定行後Python讀取行
- 27. PYTHON(PYQT) - CSV - 按行讀取
- 28. 逐行讀取csv文件
- 29. C#僅讀取特定行而不會逐行讀取
- 30. php - 獲取在特定列中包含特定值的csv行
你現在讀的所有行如何? – 2014-10-20 11:28:58