免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2729 | 回复: 4
打印 上一主题 下一主题

Vim 重排 python 代码 错误 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-30 17:58 |只看该作者 |倒序浏览
在使用Vim读代码时,用 gg=VG 重排代码很有用。可是不知道为甚对python代码,重排出错。需要设置什么,还是需要插件?

以以下代码为例:

  1. # wordfreq.py

  2. import string

  3. def compareItems((w1,c1), (w2,c2)):
  4. if c1 > c2:
  5. return - 1
  6. elif c1 == c2:
  7. return cmp(w1, w2)
  8. else:
  9. return 1

  10. def main():
  11. print "This program analyzes word frequency in a file"
  12. print "and prints a report on the n most frequent words.\n"

  13. # get the sequence of words from the file
  14. fname = raw_input("File to analyze: ")
  15. text = open(fname,'r').read()
  16. text = string.lower(text)
  17. for ch in """!"#$%&()*+,-./:;<=>?@[\\]?_'`{|}?""":
  18. text = string.replace(text, ch,' ')
  19. words = string.split(text)

  20. # construct a dictionary of word counts
  21. counts = {}
  22. for w in words:
  23. try:
  24.         counts[w] = counts[w] + 1
  25. except KeyError:
  26.         counts[w] = 1

  27. # output analysis of n most frequent words.
  28. n = input("Output analysis of how many words? ")
  29. items =counts.items()
  30. items.sort(compareItems)
  31. for i in range(n):
  32. print "%-10s%5d" % items[i]

  33. if __name__ == '__main__': main()
复制代码

论坛徽章:
0
2 [报告]
发表于 2008-10-30 20:45 |只看该作者

论坛徽章:
0
3 [报告]
发表于 2008-10-30 22:25 |只看该作者
原帖由 23号 于 2008-10-30 20:45 发表
试试这个:http://pypi.python.org/pypi/PythonTidy/1.11


多谢!

不清楚该如何使用,还望指教。

虽然不知道怎么用,却让我看到了Python的强大!

论坛徽章:
0
4 [报告]
发表于 2008-10-31 19:58 |只看该作者
原帖由 nickleeh 于 2008-10-30 22:25 发表

不清楚该如何使用,还望指教。

虽然不知道怎么用,却让我看到了Python的强大!


这两句怎么听着这么搞笑
得出Python强大的因果关系在哪呢?
用法好好看看那个网页,不是写得很清楚么

论坛徽章:
0
5 [报告]
发表于 2008-11-01 21:48 |只看该作者
原帖由 nbug 于 2008-10-31 19:58 发表


这两句怎么听着这么搞笑
得出Python强大的因果关系在哪呢?
用法好好看看那个网页,不是写得很清楚么



我得出Python的强大,是因为真的可以用python写个脚本,来实现python代码的重排。

不清楚是什么原因,我运行那个脚本,总是出错。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP