非常感谢帮助!
我有一个看起来像这样的 CSV: CSV 示例
我正在编写一个程序来检查每列是否包含正确的数据类型。例如:
- 第 1 列 - 必须具有有效的时间戳
- 列 2 - 必须保存值 2
- 第 4 列 - 必须是连续的(如果不是缺少多少个数据包)
- 第 5/6 栏 - 对值和结果进行的计算必须输入很多值
列可以位于不同的位置。
我尝试使用熊猫模块使用熊猫模块为每列提供一个"id":
import pandas as pd
fields = ['star_name', 'ra']
df = pd.read_csv('data.csv', skipinitialspace=True, usecols=fields)
print df.keys()
print df.star_name
但是,在对数据进行检查时,它似乎会感到困惑。做这样的事情的下一个最佳方法是什么?
我真的为此自杀了,任何帮助将不胜感激。
谢谢!
尝试使用 'csv' 模块。
例
import csv
with open('data.csv', 'r') as f:
# The first line of the file is assumed to contain the column names
reader = csv.DictReader(f)
# Read one row at a time
# If you need to compare with the previous row, just store that in a variable(s)
prev_title_4_value = 0
for row in reader:
print(row['Title 1'], row['Title 3'])
# Sample to illustrate how column 4 values can be compared
curr_title_4_value = int(row['Title 4'])
if (curr_title_4_value - prev_title_4_value) != 1:
print 'Values are not consecutive'
prev_title_4_value = curr_title_4_value