[英]String index out of range on otherwise fine code (dictionary of tweets) - python
I'm super desperate. 我非常绝望。
I imported a long list of tweets that looks like this for each line: 我导入了一长串的推文,每行看起来像这样:
[29.5912325, -98.370464330000004] 6 2011-08-28 19:02:31 @AyLaTayLa let me guess wing stop lol"
Each section in the tweet is separated by a tab (\\t). 推文中的每个部分都由一个制表符(\\ t)分隔。 Printing (d) and printing (x) works but the moment that I attempt to return dictlist, there's a string out of range error originating at line:
打印(d)和打印(x)都可以,但是当我尝试返回字典列表时,出现了一行超出范围的错误,该错误源自以下行:
if line[0] == '[':
I've tried indenting at different points but I either get another error or dictlist just returns the first tweet. 我尝试过缩进,但是我遇到另一个错误或字典列表只返回第一条推文。 Help!
救命!
from datetime import datetime
dictlist = []
def make_tweets():
file_name = open('/users/s/Desktop/project/all_tweets.txt', 'r')
file_name = file_name.readlines()
file_name = [x.strip() for x in file_name]
for line in file_name:
if line[0] == '[':
d ={}
x = line.split("\t")
x[0] = x[0].split(" ")
x[0][0] = float(x[0][0].strip(",""["))
x[0][1] = float(x[0][1].strip("]"))
x[2] = datetime.strptime('2011-08-28 19:02:28', '%Y-%m-%d %H:%M:%S')
x[3] = x[3].lower()
#print(x)
d['latitude'] = x[0][0]
d['longitude'] = x[0][1]
d['time'] = x[2]
d['text'] = x[3]
dictlist.append(dict(d))
#printing d works but anything beyond that
return dictlist
make_tweets() make_tweets()
When I print d, dictionaries look like this: 当我打印d时,字典看起来像这样:
{'latitude': 39.99230957, 'longitude': -75.13111973, 'time': datetime.datetime(2011, 8, 28, 19, 2, 28), 'text': '@_tweetthis what dorm r you in?'}
{'latitude': 54.10410612, 'longitude': 28.33601993, 'time': datetime.datetime(2011, 8, 28, 19, 2, 28), 'text': '@andykozik круто !!!'}
{'latitude': 19.47690987, 'longitude': -71.34471434, 'time': datetime.datetime(2011, 8, 28, 19, 2, 28), 'text': '@ninosh_flow jajajaj mi hermanasoooo'}
Pause for a moment and ponder what's actually going wrong here. 暂停片刻,然后思考这里实际出了什么问题。 You seem to have a rather voodoo approach to indentation from the way you describe things.
从描述事物的方式来看,您似乎对缩进有相当的伏都教化方法。
I'm obviously guessing here since I have no data to check it myself, but I suspect you're running into an empty line at some point. 我显然在这里猜测,因为我自己没有数据可以检查它,但是我怀疑您有时会陷入空白。
It, being empty, does not have a first element, and thus throws an IndexError presumably. 它为空,没有第一个元素,因此可能引发IndexError。 If it's something else, well, goes to show why you should include the errors too.
如果还有其他问题,那就去说明为什么也应该包含这些错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.