Python问题:打开和关闭文件返回语法错误

发布于 2024-11-08 05:24:41 字数 1729 浏览 0 评论 0原文

大家好,我发现了这个有用的 python 脚本,它允许我从网站获取一些天气数据。 我将创建一个文件和其中的数据集。

有些东西不起作用。它返回此错误。

File "<stdin>", line 42
     f.close()
     ^
SyntaxError: invalid syntax

怎么了?在这一行中我只是关闭文件! 有人可以帮我吗?

这是Python代码。

import urllib2
from BeautifulSoup import BeautifulSoup
# Create/open a file called wunder.txt (which will be a comma-delimited file)
f = open('wunder-data.txt', 'w')
# Iterate through year, month, and day
for y in range(1980, 2007):
  for m in range(1, 13):
    for d in range(1, 32):
      # Check if leap year
      if y%400 == 0:
        leap = True
      elif y%100 == 0:
        leap = False
      elif y%4 == 0:
        leap = True
      else:
        leap = False
      # Check if already gone through month
      if (m == 2 and leap and d > 29):
        continue
      elif (m == 2 and d > 28):
        continue
      elif (m in [4, 6, 9, 10] and d > 30):
        continue
      # Open wunderground.com url
      url = "http://www.wunderground.com/history/airport/KBUF/"+str(y)+ "/" + str(m) + "/" + str(d) + "/DailyHistory.html"
      page = urllib2.urlopen(url)
      # Get temperature from page
      soup = BeautifulSoup(page)
      dayTemp = soup.body.nobr.b.string
      # Format month for timestamp
      if len(str(m)) < 2:
        mStamp = '0' + str(m)
      else:
        mStamp = str(m)
      # Format day for timestamp
      if len(str(d)) < 2:
        dStamp = '0' + str(d)
      else:
        dStamp = str(d)
      # Build timestamp
      timestamp = str(y) + mStamp + dStamp
      # Write timestamp and temperature to file
      f.write(timestamp + ',' + dayTemp + '\n')
# Done getting data! Close file.
f.close()

Hi guys I've fonund this useful python script that allows me to get some weather data from a site.
I'm going to create a file and the dataset indide.

Something is not working. It returns this error.

File "<stdin>", line 42
     f.close()
     ^
SyntaxError: invalid syntax

What's wrong? In this line I'm only closing the file!
Could anyone help me please?

This is the python code.

import urllib2
from BeautifulSoup import BeautifulSoup
# Create/open a file called wunder.txt (which will be a comma-delimited file)
f = open('wunder-data.txt', 'w')
# Iterate through year, month, and day
for y in range(1980, 2007):
  for m in range(1, 13):
    for d in range(1, 32):
      # Check if leap year
      if y%400 == 0:
        leap = True
      elif y%100 == 0:
        leap = False
      elif y%4 == 0:
        leap = True
      else:
        leap = False
      # Check if already gone through month
      if (m == 2 and leap and d > 29):
        continue
      elif (m == 2 and d > 28):
        continue
      elif (m in [4, 6, 9, 10] and d > 30):
        continue
      # Open wunderground.com url
      url = "http://www.wunderground.com/history/airport/KBUF/"+str(y)+ "/" + str(m) + "/" + str(d) + "/DailyHistory.html"
      page = urllib2.urlopen(url)
      # Get temperature from page
      soup = BeautifulSoup(page)
      dayTemp = soup.body.nobr.b.string
      # Format month for timestamp
      if len(str(m)) < 2:
        mStamp = '0' + str(m)
      else:
        mStamp = str(m)
      # Format day for timestamp
      if len(str(d)) < 2:
        dStamp = '0' + str(d)
      else:
        dStamp = str(d)
      # Build timestamp
      timestamp = str(y) + mStamp + dStamp
      # Write timestamp and temperature to file
      f.write(timestamp + ',' + dayTemp + '\n')
# Done getting data! Close file.
f.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我早已燃尽 2024-11-15 05:24:41

看起来你那里有空格问题。检查文件的空白 - 查看空格和制表符在哪里。如果文件中同时存在制表符和空格,请将它们全部转换为空格。

f.close 应与 f = open('wunder-data.txt', 'w') 具有相同的缩进级别

Looks like you have a whitespace problem in there. Check the whitespace of the file - see where spaces and tabs are. If there are both tabs and spaces in the file, convert them all to spaces.

f.close should be at the same indentation level as f = open('wunder-data.txt', 'w')

も星光 2024-11-15 05:24:41

我刚才在 3.8 中遇到了类似的错误,显示语法错误。事实证明,我的语法错误是突出显示行上方的代码中缺少右括号。因此,第 42 行不是 Question Putter 应该查看的内容。也许,代码上面的某处实际上存在语法错误。

I had a similar Error showing syntax error in 3.8 just now. And it turns out, my syntax error was a missing closing parenthesis in code above the highlighted line. So, line 42 is not what Question Putter should be looking at. Perhaps, there is actually a syntax error somewhere above in the code.

放手` 2024-11-15 05:24:41

带有 f.close() 的行不是第 42 行,所以您确定这是给出错误的代码吗?

另外,Python 似乎处理在 stdin 上收到的程序,这是你的意图吗?

The line with the f.close() isn't line 42, so are you sure this is the code that gives the error?

Also, Python seems to process a program received on stdin, is this your intention?

舞袖。长 2024-11-15 05:24:41

删除代码中的行,直到语法错误消失。然后你就能缩小问题范围。

Delete lines in your code until the syntax error goes away. Then you'll be able to narrow the problem.

逆夏时光 2024-11-15 05:24:41

我今天 2020 年(十月)遇到了同样的问题,我刚刚检查了 close() 函数上面的代码。我发现我之前没有关闭括号。因此错误出现在函数上方的代码中,而不是指定的实际行中。

I had the same problem today 2020(October), and I just checked the code above the close() function. I found I hadn't closed a bracket earlier. So the error is in the code above the function and not the actual line specified.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文