当我在Python上运行代码时,我的代码总是显示一个错误:“太多而无法解包”。如何修复此错误?

2024-03-29 12:16:44 发布

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

这是我的密码:

big_str = "[41.386263640000003, -81.494450689999994]\t6\t2011-08-28 19:02:28\tyay. little league world series!\n[42.531041999999999, -82.90854831]\t6\t2011-08-28 19:02:29\ti'm at holiday inn express suites & hotel roseville mi (31900 little mack ave., at masonic blvd., roseville) \n[39.992309570000003, -75.131119729999995]\t6\t2011-08-28 19:02:29\t@_tweetthis what dorm r you in?\n[54.104106119999997, 28.336019929999999]\t6\t2011-08-28 19:02:29\t@andykozik круто !!!\n[25.787949600000001, -80.132949600000003]\t5\t2011-09-03 05:40:14\tpizza rustica #ftw"

# This breaks up a string of tweets into lists
def break_into_records(big_mess):
    rows = big_mess.split('\n')
    records = []
    for row in rows:
        records.append(row.split('\t'))
    for i in range(len(records)):
        records[i][0] = records[i][0].replace(' ','').strip('[]').split(',')
    return records

# This will loop over that list, and call a draw
# every time a word pops up
# as well as give you a word count
def count_words(word, data_structure):
    count = 0
    for loc, id, time, message in data_structure:
        if word in message:
            drop_pin(loc) # we pass cords to drop pin, no work to do
            count += message.count(word)
    return count

def drawGpsPoint(x):
  y = drop_pin(x)
  return y

def drop_pin(location):
    # this function needs to map the GPS cord to an x/y
    # so it can draw
    # but it has the GPS locs.
    lat, lon = location
    lat, lon = float(lat), float(lon)
    x = 500 - ((lat + 180) * 500.0/360)
    y = 500 - ((lon + 180) * 500.0/360)
    return lat, lon

a = break_into_records(big_str)
for x in a:
  drawGpsPoint(x)
  print("All Tweets Loaded")
  r = raw_input("Enter search word: ")

它在每次到达落针(位置)功能中的lat,lon时显示错误。我需要先分离变量,然后再通过该行运行它吗?你知道吗


Tags: toinforreturndefcountpindrop
1条回答
网友
1楼 · 发布于 2024-03-29 12:16:44

您的break_into_records函数正在将字符串信息拆分为list个元素,坐标是第一个元素。你知道吗

因此,要更正该错误,需要将drawGpsPoint(x)替换为:

drawGpsPoint(x[0])
网友
2楼 · 发布于 2024-03-29 12:16:44

你的break_into_records没有达到你的预期。第一次给drop_pin(location)打电话时,您的位置是

[['41.386263640000003', '-81.494450689999994'], '6', '2011-08-28 19:02:28', 'yay. little league world series!']

请注意,我是通过在尝试分配它之前打印出location来确定的。你知道吗

“太多值无法解包”错误是由于尝试将其分配给lat, long是没有意义的。你知道吗

您可能只想返回break_into_records函数的第一部分,或者只想对这些字符串的第一部分进行操作。你知道吗

相关问题 更多 >