使用XLRD而不是CSV

2024-04-29 05:40:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我完成了程序的编辑,这样它就可以读取CSV文件并将数据导出到PSSE中。现在,我尝试使用XLRD包来读取excel数据,就像我使用CSV模块一样,我不知道如何使用下面的示例来实现它。我看了其他的例子,但对我没什么帮助。任何帮助都将不胜感激。在

import os, sys

PSSE_LOCATION = r"C:\Program Files (x86)\PTI\PSSE33\PSSBIN"
sys.path.append(PSSE_LOCATION)
os.environ['PATH'] = os.environ['PATH'] + ';' + PSSE_LOCATION
import psspy
import redirect
import csv
psspy.throwPsseExceptions = True

from Tkinter import *
import tkFileDialog
import tkSimpleDialog
import tkMessageBox



 STUDY_CASE = 'C:\Users\RoszkowskiM\Documents\Cases\Final\ceii_Case1_SUM_2017_5050_MMWG16PF_FINAL.sav'

LOAD_GEN_DATAFILE = 'C:\Users\RoszkowskiM\Documents\CSV Files\ASTECOR_TLA.csv'

psspy.psseinit(10000)
psspy.case(STUDY_CASE)



data = list(csv.reader(open(LOAD_GEN_DATAFILE)))
mydict = {}
for row in data:
year,location,bus,change,isload = row[0:5]
# convert the types from string to Python numbers

change= float(change)
bus = int(bus)

 #If this is a year not seen before, add it to the dictionary
 if year not in mydict:
    mydict[year] = {}

 busses_in_year = mydict[year]
 if location not in busses_in_year:
     busses_in_year[location] = []

 #Add the bus to the list of busses that stop at this location
 busses_in_year[location].append((bus, change,isload))


 # assume CSV has columns as described in the doc string
 year = raw_input("Select Year of Study: ")

 location = raw_input(" Select the number associated to the TLA Pocket Location:")

 if year in mydict and location in mydict[year]:  
  busses_in_year = mydict[year]
  print("Here are all the busses at that location for that year: ")
  for bus in busses_in_year[location]:
    print(bus)


 else:
    print("Invalid Year or Location")


if isload.isdigit() and int(isload):    
    psspy.bsys(1,0,[0.0,0.0],0,[],1,[bus],0,[],0,[])
    psspy.scal_2(1,0,1,[0,0,0,0,0],[0.0,0.0,0.0,0.0,0.0,0.0,0.0])
    psspy.scal_2(0,1,2,[0,1,0,1,0],[change,0.0,0,-.0,0.0,-.0,0])

Tags: csvthetoinimportiflocationchange