Python CSV文件

时间:2017-01-13 00:44:33

标签: python

我在查看csv文件中的数据后得到了一些帮助 csv文件已经列出

week_no,w_day,t_time,set_point
1,Monday,4:45,17
1,Monday,5:15,15
1,Monday,8:00,17

Python文件是

import datetime
import os
import time
import csv
today_day = date.today().strftime("%A") # get todays day of the week
curent_time = datetime.now().strftime("%H:%M") # get hours and minuites
curent_week = 1 # set for csv read file
heating_set = 15 # set for set point

with open('c:\Heating_times.csv', newline='') as f:
    reader = csv.reader(f)
    for row in reader:
        print(row)
f.close()

在屏幕上打印文件的内容

我想要做的是查看列中的数据,如果所有行都匹配,则将值复制到heating_set

if curent_week = week_no and today_day = w_day and curent_time = t_time 
    heating_set = set_point from the csv file column 

我已经找到了这个例子,但我找不到任何

感谢您的帮助

斯图尔特

新代码

import datetime
import os
import time
import csv
from datetime import date
from datetime import datetime

#today_day = date.today().strftime("%A") # get todays day of the week
#current_time = datetime.now().strftime("%H:%M") # get hours and minuites
current_week = 1 # initial set for csv read file
heat_setting = 15 # initial set for csv read file

while True: # setup for heating on timers
    today_day = "Monday"
    current_time = "8:00"
#    today_day = date.today().strftime("%A") # get todays day of the week
#    current_time = datetime.now().strftime("%H:%M") # get hours and minuites

    with open('F:\Heating_times.csv', newline='') as f:
        reader = csv.DictReader(f)
        for row in reader:
            print (row)
            if row['week_no'] == current_week and row['w_day'] == today_day and row['t_time'] == current_time:
                heat_setting = row['set_point']
    f.close()
    print (current_week)
    print (today_day)
    print (current_time)
    print(heat_setting)
    time.sleep(10) 

输出窗口读取

OrderedDict([('week_no', '1'), ('w_day', 'Monday'), ('t_time', '8:00'), ('set_point', '17')])
1
Monday
8:00
15

2 个答案:

答案 0 :(得分:0)

因此,您应该使用csv模块中的DictReader类,而不是简单地读取行:

with open(...) as f:
    reader = csv.DictReader(f)
    for row in reader:
        if row['week_no'] == current_week and row['w_day'] == today_day and row['t_time'] == current_time:
            heat_setting = row['set_point']

答案 1 :(得分:0)

需要将current_week和heat_setting设置为字符串

import datetime
import os
import time
import csv
from datetime import date
from datetime import datetime

#today_day = date.today().strftime("%A") # get todays day of the week
#current_time = datetime.now().strftime("%H:%M") # get hours and minuites
current_week = "1" # initial set for csv read file
heat_setting = "15" # initial set for csv read file

while True: # setup for heating on timers
    today_day = "Monday"
    current_time = "8:00"
#    today_day = date.today().strftime("%A") # get todays day of the week
#    current_time = datetime.now().strftime("%H:%M") # get hours and minuites

    with open('F:\Heating_times.csv', newline='') as f:
        reader = csv.DictReader(f)
        for row in reader:
            print (row)
            if row['week_no'] == current_week and row['w_day'] == today_day and row['t_time'] == current_time:
                heat_setting = row['set_point']
    f.close()
    Set_temp = int(heat_setting)
    print (current_week)
    print (today_day)
    print (current_time)
    print(Set_temp)
    time.sleep(10) 

输出窗口

OrderedDict([('week_no', '1'), ('w_day', 'Monday'), ('t_time', '8:00'), ('set_point', '17')])
1
Monday
8:00
17

感谢您指点我正确的方向