简体   繁体   中英

Assigning variables to parts of a text file

I have a line from a text file that goes as follows:

[41.298669629999999, -81.915329330000006] 6 2011-08-28 19:02:36 Work needs to fly by ... I'm so excited to see Spy Kids 4 with then love of my life ... ARREIC

I am trying to assign different parts of this line to specific variables with this code:

latitude = 0
longitude = 0
unused1 = 0
unused2 = 0
unused3 = 0
tweetWordList = []
for line in tweetFile:
    line = line.rstrip()
    longitude,latitude,unused1,unused2,unused3,tweetWordList = line.split()

I am trying to get the chunk of text from the tweet into the tweetWordList but I get an error saying that there are too many values to unpack. How do I divide this line so that the writing goes into the list I created?

I already read in the file and the rest of the program to this point works fine.

It's because you're splitting on spaces, so all of the text gets split into list items as well. If the formatting is consistent, I'd suggest splitting on list indices:

>>> line = "[41.298669629999999, -81.915329330000006] 6 2011-08-28 19:02:36 Work needs to fly by ... I'm so excited to see Spy Kids 4 with then love of my life ... ARREIC"
>>> splitline = line.split()
>>> longitude = splitline[0].replace('[', '').replace(',', '')
>>> latitude = splitline[1].replace(']', '')
>>> tweetWordList = ' '.join(splitline[5:])

Alternatively, you could do it with a regex pattern:

>>> import re
>>> latitude, longitude, tweetWordList = re.findall("^\[([\d.]+), ([\d\-.]+)\] [\d] [\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2} ([A-Za-z0-9 .']+)", line)[0]

You'll need to play around with the regex pattern to match your text properly, but that's the gist of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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