我的代码帮助中有错误,请python



这是我的代码,它不断地出现相同的错误代码,我不知道为什么会出现错误,也不知道它与我的代码有什么关系——任何信息都会有所帮助!我只是不太擅长处理错误

import urllib.request
import codecs
import matplotlib.pyplot as pyplot
import copy
from time import gmtime, strftime
from urllib.request import urlopen
def readInfo(filename):
    myFile = open(filename,'r')
    data=myFile.readlines()
    namelist=[i.split(' ', 1)[0] for i in data]
    balance = list()
    for item in data:
        name = item.split()[0]
        amount = float(item.split()[1])
        balance.append([name, amount])
    print("n",namelist,"c",balance)
    return (namelist, balance)
def fetch(url):
    def find_element(line, s_pattern, e_pattern, position=0):
        shift = len(s_pattern)
        start = line.find(s_pattern, position) + shift
        position = start
        end = line.find(e_pattern, position)
        return (line[start:end], position)
    html = urlopen(url)
    records = []
    i = 0
    for line in html.readlines():
        line = line.decode()
        if "<tr><td>" not in line:
             continue  # skip if line don't contain rows
        if "Currency" in line:
            continue  # skip header
        start = "<tr><td>"
        end = "</td>"
        element, start_pos = find_element(line, start, end)
        records.append([element])
        start = "<td>"
        values = []
        for x in range(2):
            element, start_pos = find_element(line, start, end, start_pos)
            values.append(element)
        records[i].append(values)
        i = i + 1
    print(records)
    return(records)
def findCurrencyValue(records, currency_name):
    d = dict(records)
    print(d[currency_name])
    return(d[currency_name])
def transaction(filename, namelist, orgBalance, url):
    exchange_info= fetch(url)
    #Read each line from transactions.txt
    myFile = open(filename,'r')
    data=myFile.readlines()
    #Check which company is conducting transactions
    bank = dict(orgBalance)
    for line in data:
        company,action,currency,ammount = line.split()
        do_something(company,action,currency,ammount,bank)
    #If BUY, then convert the amount of foreign currency to USD
    #and subtract the calculated amount
    def find_currency_rate(currency):
    # locate the curency name in the text body and find the last <td></td> value in that row...
        return float(last_td_cell_of_row)
    def convert_to_usd(currency,amount):
        currency_rate = find_currency_rate(currency)
        return amount*currency_rate
    def do_something(company_name,action,currency_code,amount,bank):
        amount_in_usd = convert_to_usd(currency_code,amount)
        if action == "BUY":
            bank[company_name] = bank[company_name] - amount_in_usd
        else: # else we sell and add the funds to our bank
            bank[company_name] = bank[company_name] + amount_in_usd
def main():
    #get the namelist and original balance for all companies
    filename1 = "balance.txt"
    namelist, orgBalance = readInfo(filename1)
    #specifies the URL
    url = "https://www.cs.purdue.edu/homes/jind/exchangerate.html"
    #calculate the current balance for all companies
    balance = copy.deepcopy(orgBalance)
    filename2 = "transactions.txt"
    curBalance = transaction(filename2, namelist, balance, url)
    #output the value for the original balance for each company
    #this output should be a list of lists
    print("Original Balance of each company is: ", orgBalance)
    #output the value for the current balance for each company
    #this output should be a list of lists
    print("Current Balance of each company is: ", curBalance)
    #call your bar graph plotting function
    plotBarGraph(orgBalance, curBalance)
    #call your pie graph plotting function
    plotPieChart(curBalance)    
main()

当我运行代码时,我得到了以下错误,我需要帮助修复

Traceback (most recent call last):
  File "C:UsersnoahdDesktopproject3project3-skeleton.py", line 134, in <module>
    main()
  File "C:UsersnoahdDesktopproject3project3-skeleton.py", line 123, in main
    curBalance = transaction(filename2, namelist, balance, url)
  File "C:UsersnoahdDesktopproject3project3-skeleton.py", line 63, in      transaction
    company,action,currency,ammount = line.split()
ValueError: too many values to unpack (expected 4)

似乎当你执行line.split()时,它返回的元素比需要的多,应该只有4个:公司、行动、货币、弹药数量。尽管如此,您可以尝试:

try:
    company,action,currency,ammount = line.split()
except:
    print "DEBUG: split:",line.split()

并检查错误在哪里

尝试:

transaction,company,action,currency,ammount,_ = line.split()

最新更新