鱼C论坛

 找回密码
 立即注册
查看: 2450|回复: 7

[已解决]怎么编写一个现实文档中每个字有多少个的程序

[复制链接]
发表于 2017-7-5 08:59:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import os
os.chdir('F:/')
list1 = []
str1 = open('poem.txt')
for each in str1:
    if each not in list1:
        if each == '\n':
            print('\\n',str1.count(each))
        else:
            print(each,str1.count(each))
        list1.append(each)
            
这是我的程序但是运行以后现实的是 print('\\n',str1.count(each))
AttributeError: '_io.TextIOWrapper' object has no attribute 'count'

请大神们指点
最佳答案
2017-7-5 13:47:14
按2楼的方法是可行的。
  1. list1 = []
  2. file = open('poem.txt')
  3. str1 = file.read()
  4. for each in str1:
  5.     if each not in list1:
  6.         if each == '\n':
  7.             print('\\n',str1.count(each))
  8.         else:
  9.             print(each,str1.count(each))
  10.         list1.append(each)
  11. file.close()
复制代码

  1. S 4
  2. h 31
  3. a 37
  4. l 23
  5.   114
  6. I 1
  7. c 9
  8. o 44
  9. m 22
  10. p 4
  11. r 28
  12. e 61
  13. t 39
  14. s 38
  15. u 13
  16. ' 8
  17. d 19
  18. y 8
  19. ? 1
  20. \n 14
  21. T 1
  22. v 5
  23. n 31
  24. . 3
  25. R 1
  26. g 10
  27. w 4
  28. i 26
  29. k 1
  30. b 3
  31. f 10
  32. M 1
  33. , 6
  34. A 3
  35. x 1
  36. ; 3
  37. B 2
  38. N 2
  39. D 1
  40. W 1
  41. : 1
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-7-5 09:21:04 | 显示全部楼层
str1 = open('poem.txt', 'r')
str1=str1.read()
str1.close()

要不然你就直接用with open('abc.txt','r') as str1: 这种就一次性读取,不要read() 和close()
https://zhidao.baidu.com/question/2117398126862184987.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-5 10:31:16 | 显示全部楼层
ButcherRabbit 发表于 2017-7-5 09:21
str1 = open('poem.txt', 'r')
str1=str1.read()
str1.close()

我修改了可是还是显示no attribute for count,我的文件里是一首英文诗,是不是不能用str,
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-5 11:03:22 | 显示全部楼层
夜深听雨 发表于 2017-7-5 10:31
我修改了可是还是显示no attribute for count,我的文件里是一首英文诗,是不是不能用str,

你看看你的python 是2的版本还是3的版本,还有英文诗能上传一下么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-5 13:21:50 | 显示全部楼层
ButcherRabbit 发表于 2017-7-5 11:03
你看看你的python 是2的版本还是3的版本,还有英文诗能上传一下么

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>   这是显示的版本

Shall I compare thee to a summer's day?
Thou art more lovely and more temperate.
Rough winds do shake the darling buds of May,
And summer's lease hath all too short a date.
Sometime too hot the eye of heaven shines,
And often is his gold complexion dimm'd;
And every fair from fair sometime declines,
By chance or nature's changing course untrimm'd;
But thy eternal summer shall not fade
Nor lose possession of that fair thou ow'st;
Nor shall Death brag thou wander'st in his shade,
When in eternal lines to time thou grow'st:
So long as men can breathe or eyes can see,
So long lives this, and this gives life to thee. 这是诗,麻烦你了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-5 13:47:14 | 显示全部楼层    本楼为最佳答案   
按2楼的方法是可行的。
  1. list1 = []
  2. file = open('poem.txt')
  3. str1 = file.read()
  4. for each in str1:
  5.     if each not in list1:
  6.         if each == '\n':
  7.             print('\\n',str1.count(each))
  8.         else:
  9.             print(each,str1.count(each))
  10.         list1.append(each)
  11. file.close()
复制代码

  1. S 4
  2. h 31
  3. a 37
  4. l 23
  5.   114
  6. I 1
  7. c 9
  8. o 44
  9. m 22
  10. p 4
  11. r 28
  12. e 61
  13. t 39
  14. s 38
  15. u 13
  16. ' 8
  17. d 19
  18. y 8
  19. ? 1
  20. \n 14
  21. T 1
  22. v 5
  23. n 31
  24. . 3
  25. R 1
  26. g 10
  27. w 4
  28. i 26
  29. k 1
  30. b 3
  31. f 10
  32. M 1
  33. , 6
  34. A 3
  35. x 1
  36. ; 3
  37. B 2
  38. N 2
  39. D 1
  40. W 1
  41. : 1
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2017-7-5 14:41:37 | 显示全部楼层
str1 = file.read()

学习了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-5 15:15:00 | 显示全部楼层
冬雪雪冬 发表于 2017-7-5 13:47
按2楼的方法是可行的。

懂啦,谢谢 大神
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-28 09:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表