将变量分配给文本文件的各个部分

时间:2018-11-12 22:21:25

标签: python parsing

我有一个文本文件中的一行,内容如下:

[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

我正在尝试使用以下代码将此行的不同部分分配给特定变量:

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()

我正在尝试将tweet中的文本块放入tweetWordList中,但是我收到一条错误消息,提示要解压缩的值太多。我如何划分这一行,以便将文字写到我创建的列表中?

我已经读入文件,并且程序的其余部分到此为止都工作正常。

1 个答案:

答案 0 :(得分:1)

这是因为您要在空格上进行拆分,所以所有文本也都将拆分为列表项。如果格式一致,则建议拆分列表索引:

>>> 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:])

或者,您可以使用正则表达式模式:

>>> 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]

您需要使用正则表达式模式来正确匹配文本,但这就是要点。

相关问题