鱼C论坛

 找回密码
 立即注册
查看: 2159|回复: 3

[已解决]各位大佬帮帮忙,谢谢啦

[复制链接]
发表于 2020-10-27 20:21:29 | 显示全部楼层 |阅读模式

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

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

x
  1. file = str(open('D:\\string1.txt',encoding='UTF-8'))

  2. print(file)

  3. def search(str1):
  4.     length = len(file)
  5.     count = 0
  6.     for each in range(length-1):
  7.         if str1 == file[each]:
  8.             count += 1
  9.             continue
  10.         else:
  11.             continue
  12.     print(count)

  13. search(']')
复制代码



各位大佬好,我的这个代码怎么打印出来一直是零呢?小甲鱼的第20次零基础课的课后题.txt文件
最佳答案
2020-10-27 20:47:00
DoubleS、Zhao 发表于 2020-10-27 20:42
如果我想把这个文件里面的内容变成字符串怎么办啊???
  1. file = list(open('D:\\string1.txt',encoding='UTF-8'))
  2. string = ''.join(file)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-10-27 20:30:47 | 显示全部楼层
file = str(open('D:\\string1.txt',encoding='UTF-8'))得到的是文件的句柄,改成
file = list(open('D:\\string1.txt',encoding='UTF-8')) 试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-10-27 20:42:28 | 显示全部楼层
冬雪雪冬 发表于 2020-10-27 20:30
file = str(open('D:\\string1.txt',encoding='UTF-8'))得到的是文件的句柄,改成
file = list(open('D:\\ ...

如果我想把这个文件里面的内容变成字符串怎么办啊???
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-27 20:47:00 | 显示全部楼层    本楼为最佳答案   
DoubleS、Zhao 发表于 2020-10-27 20:42
如果我想把这个文件里面的内容变成字符串怎么办啊???
  1. file = list(open('D:\\string1.txt',encoding='UTF-8'))
  2. string = ''.join(file)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-28 21:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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