简体   繁体   English

Python / CSV读取数据

[英]Python/CSV Read Data

My program shown below has the goal of outputting statements based off two criteria:( Year and Location ) Once that was accomplished, my next goal is to match year with Yearlink and Location with location . 如下图所示我的程序有输出语句的目标基于关闭两个条件:( YearLocation ),一旦完成了,我的下一个目标是,以配合yearYearlinkLocationlocation location and year are both input statements. locationyear都是输入语句。 The output should be a statement that outputs values from that row. 输出应该是输出该行值的语句。 For some reason, I always receive an error about too many values to unpack. 由于某些原因,我总是收到一个错误,提示要解包的值太多。 Also when I fix that error, it never outputs anything for that section of the program. 同样,当我修复该错误时,它永远不会为程序的该部分输出任何内容。 The first part of program is outputting correctly. 程序的第一部分输出正确。 Second part is either giving me Value Error or not outputting at all. 第二部分要么给我值错误,要么根本不输出。 My excel sheet is posted if anyone wants a better idea of what I am talking about. 如果有人想更好地了解我在说什么,就会发布我的Excel工作表。 Excel表格 Anyway I tried everything but nothing was working for me. 无论如何,我尝试了一切,但没有任何工作对我有用。

Traceback (most recent call last):
File "C:/Users/RoszkowskiM/Desktop/win4.py", line 134, in <module>
for From,To,Max,Min in data:
ValueError: too many values to unpack

- -

LOAD_GEN_DATAFILE = 'C:\Users\RoszkowskiM\Desktop\Data_2016.csv' # CSV File to Read
    # read the entire CSV into Python.
    # CSV has columns starting with Year,busnum,busname,scaled_power,tla,location
    data = list(csv.reader(open(LOAD_GEN_DATAFILE)))
    mydict = {}
    for row in data:
        Year,busnum,busname,scaled_power,tla,Location,Yearlink,From,To,Max,Min = row[0:12]



    #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((busnum,busname,scaled_power))
#-------------------------------------------------------------------------------



#-------------------------------------------------------------------------------------------------------------------------------------
#User Input Statement
year = raw_input("Please Select Year of Study: ")
print("\n")

commands = ["Millwood-Buchanan", "Astoria-East-Corona", "Bronx", "DUNWOODIE-North-Sherman_Creek",
            "Vernon", "Greenwood-StatenIsland","West_49th","East_13th","Staten_Island","East_River",
            "East_View","DUNWOODIE-SOUTH","Corona-Jamaica","Astoria-East-Corona-Jamaica",
            "Astoria-West-Queensbridge-Vernon","Astoria-West-Queensbridge"]
max_columns = 50

for index, commands in enumerate(commands):
    stars_amount = max(max_columns - len(commands), 0)
    row = "# {} {}({})".format(commands, "." * stars_amount, index + 1)
    print(row)
location=raw_input(" \n The list above show the TLA Pockets as well as the ID numbers assigned to them ()\n\n Please enter the ID #: ")
print("\n")
Year=year
Location=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 and the new LOAD TOTAL: ")
    print("\n")


#Busnum, busname,scaled_power read from excel sheet matching year and location

for busnum,busname,scaled_power in busses_in_year[Location]:
    scaled_power= float(scaled_power)
    busnum = int(busnum)
    print('Bus #: %d\t' % busnum ,'Area Station: %s\t'% busname,'New Load Total: %d MW\t' % scaled_power)
else:
        exit

- -

 for row in data:
    Year,busnum,busname,scaled_power,tla,Location,Yearlink,From,To,Max,Min = row[0:11]

    if Yearlink==year and Location==location:
            for From,To,Max,Min in data:
                From=int(From)
                To=int(To)
                Max=float(Max)
                Min=float(Min)
                print('From Bus #: %d\t' % From ,'To Bus #: %d\t'% To,'VMAX: %d pu\t' % Max, 'VMIN: %d pu\t' % Min)                                                                

else:                                                                                                               
    exit

You are iterating over data to get the row s, but then you iterate again over data to get From , To , Max and Min , which you had already unpacked. 您正在遍历data以获取row s,但是然后再次遍历data以获取已解包的FromToMaxMin That line doesn't make sense. 那条线没有意义。 This should work: 这应该工作:

for row in data:
    data_location, year_link, from_, to, max_value, min_value = row[5:11]
    output = 'From Bus #: {}\tTo Bus #: {}\tVMAX: {} pu\tVMIN: {} pu\t'
    if year_link == year and data_location == location:
        print(output.format(from_, to, max_value, min_value))

I simplified a bit and changed the variable names, but the only thing you have to do is remove that spurious for line. 我简化了一下,改变了变量名,但你必须做的唯一的事情就是删除虚假for线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM