<p>您只需要插入一些跟踪<strong>打印</strong>语句来显示发生了什么。我是这样做的:</p>
<pre><code>for line in tinputfile:
twoparts = line.split("]") #splits tweet file by ] creating a location and tweet parts, tweets are ignored for now
startlocation = (twoparts[0]) #takes the first part (the locations)
print (" -")
print ("twoparts", twoparts)
print ("startlocation", startlocation)
def testing(startlocation):
for line in startlocation:
print ("line", line)
intlocation = line.split("[") #then gets rid of the "[" at the beginning of the locations
print ("intlocation", intlocation)
testing(startlocation)
</code></pre>
<p>。。。找到了一条线索,开头是:</p>
<pre><code> -
twoparts ['[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\n"]
startlocation [41.298669629999999, -81.915329330000006
-
twoparts ['[33.702900329999999, -117.95095704000001', " 6 2011-08-28 19:03:13 Today is going to be the greatest day of my life. Hired to take pictures at my best friend's gparents 50th anniversary. 60 old people. Woo.\n"]
startlocation [33.702900329999999, -117.95095704000001
-
twoparts ['[38.809954939999997, -77.125144050000003', ' 6 2011-08-28 19:07:05 I just put my life in like 5 suitcases\n']
startlocation [38.809954939999997, -77.125144050000003
-
twoparts ['[27.994195699999999, -82.569434900000005', ' 6 2011-08-28 19:08:02 @Miss_mariiix3 is the love of my life\n']
startlocation [27.994195699999999, -82.569434900000005
line [
intlocation ['', '']
line 2
intlocation ['2']
line 7
</code></pre>
<p><strong>分析:</strong></p>
<p>有两个基本问题:</p>
<ol>
<li>处理语句<strong>testing(startlocation)</strong>位于循环之外,因此它只使用最后一个输入行。你知道吗</li>
<li>正如您在“twoparts”的输出中所看到的,您所需的坐标仍然是<em>string</em>格式,而不是浮点列表。你需要把支架剥下来,把它们分开。<em>然后将它们转换为float。在当前表单中,当您遍历<strong>intlocation</strong>时,您遍历的是字符串的字符,而不是两个float。你知道吗</li>
</ol>
<p>另外:为什么要在循环中定义函数?这将在每次执行时重新定义函数。将它移到主程序之前;这是表现良好的函数的所在。:-)</p>
<hr/>
<p><strong>添加了关于第2点的信息:</strong></p>
<p>让我们使用示例输入的最后一行,逐步浏览您的代码。
从tinputfile中的行的循环顶部开始</p>
<pre><code>twoparts = line.split("]")
</code></pre>
<p><strong>两部分现在是一对元素,两个字符串:</p>
<pre><code>['[27.994195699999999, -82.569434900000005',
' 6 2011-08-28 19:08:02 @Miss_mariiix3 is the love of my life\n']
</code></pre>
<p>然后将<strong>startlocation</strong>设置为第一个元素:</p>
<pre><code>'[27.994195699999999, -82.569434900000005'
</code></pre>
<p>然后是对函数<strong>测试的冗余重新定义,它不会产生任何变化。下一个语句调用<strong>测试<strong>;我们进入例程。你知道吗</p>
<pre><code>testing(startlocation)
for line in startlocation:
</code></pre>
<p>这里重要的一点是,<strong>shortocation</strong>是一个<em>字符串</em>:</p>
<pre><code>'[27.994195699999999, -82.569434900000005'
</code></pre>
<p>。。。所以当你执行这个循环时,你迭代字符串,一次一个字符。你知道吗</p>
<p><strong>更正:</strong></p>
<p>老实说,我不知道测试应该做什么。
看起来你所需要做的就是剥掉那个支架:</p>
<pre><code>intlocation = startlocation.split('[')
</code></pre>
<p>。。。或者只是</p>
<pre><code>intlocation = startlocation[1:]
</code></pre>
<p>相反,如果希望将<em>float</em>值作为两个元素的列表,(a)去掉上面的括号,在逗号处拆分元素,然后转换为float:</p>
<pre><code>intlocation = [ float(x) for x in startlocation[1:].split(',') ]
</code></pre>