楼主: chenggzs
766 6

[其他] 求指导编写一个python代码 [推广有奖]

  • 0关注
  • 2粉丝

博士生

3%

还不是VIP/贵宾

-

威望
0
论坛币
622 个
通用积分
22.3066
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
967 点
帖子
58
精华
0
在线时间
346 小时
注册时间
2012-3-24
最后登录
2023-10-31

100论坛币
求指导编写一个python代码,要实现:
        1、一次性遍历指定文件夹内(包括下面的所有子文件夹)的所有txt文件;
        2、将所有txt文件汇总入一个excel表格:表格的第一列为序数;第二列为单个txt文件的文件名,第三列为单个txt文件内字符(不包括空格)的数量。
        刚开始学python,尝试了多次没有成功,希望有人能指导我,请会编程的同学把代码(最好带注释)发给我,感谢!


最佳答案

dgafqasq 查看完整内容

import os import glob s = [] lst = [] for filename in glob.glob(r'D:\SteamLibrary\*.txt'):#举个例子~遍历问目录下所有txt文件 s.append(filename)#存文件名 try: f = open(filename) contents = f.read() except NameError: f.close() else: words = contents.rstrip() num_words = len(words) lst.append(num_words)#存字数 f.clo ...
关键词:文件夹 文件名 一次性
沙发
dgafqasq 发表于 2019-3-5 20:38:53 |只看作者 |坛友微信交流群
import os
import glob
s = []
lst = []
for filename in glob.glob(r'D:\SteamLibrary\*.txt'):#举个例子~遍历问目录下所有txt文件
    s.append(filename)#存文件名
    try:
        f = open(filename)
        contents = f.read()
    except NameError:
        f.close()
    else:
        words = contents.rstrip()
        num_words = len(words)
        lst.append(num_words)#存字数
        f.close()

import xlwt
#需要处理excel的库,cmd打开复制pip install xlwt
workbook = xlwt.Workbook(encoding='utf-8')
booksheet = workbook.add_sheet('Sheet 1', cell_overwrite_ok=True)
for i in range(len(s)):#递归把数据写进去
    rowdata = [i+1,s[i],lst[i]]
    for m in range(3):
        booksheet.write(i,m,rowdata[m])
workbook.save('test_xlwt.xls')  

已有 1 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
admin_kefu + 40 + 5 + 5 + 5 热心帮助其他会员

总评分: 论坛币 + 40  学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

使用道具

藤椅
happy2022stf 在职认证  发表于 2019-3-6 11:09:34 |只看作者 |坛友微信交流群
加油,我也刚学,共同努力

使用道具

板凳
chenggzs 发表于 2019-3-6 13:08:52 |只看作者 |坛友微信交流群
dgafqasq 发表于 2019-3-5 22:49
import os
import glob
s = []
C:\Python37-32\python.exe C:/Users/Lenovo/PycharmProjects/content/test.py
Traceback (most recent call last):
  File "C:/Users/Lenovo/PycharmProjects/contenti/test.py", line 9, in <module>
    contents = f.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 12: illegal multibyte sequence

Process finished with exit code 1

使用道具

报纸
chenggzs 发表于 2019-3-6 13:08:58 |只看作者 |坛友微信交流群
dgafqasq 发表于 2019-3-5 22:49
import os
import glob
s = []
C:\Python37-32\python.exe C:/Users/Lenovo/PycharmProjects/content/test.py
Traceback (most recent call last):
  File "C:/Users/Lenovo/PycharmProjects/contenti/test.py", line 9, in <module>
    contents = f.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 12: illegal multibyte sequence

Process finished with exit code 1

使用道具

地板
chenggzs 发表于 2019-3-6 13:09:24 |只看作者 |坛友微信交流群
dgafqasq 发表于 2019-3-5 22:49
import os
import glob
s = []
报错了,请问如何处理呢

使用道具

7
chenggzs 发表于 2019-3-6 13:21:05 |只看作者 |坛友微信交流群
f = open(filename) 改成了 f = open(filename,encoding = 'gbk',errors = 'ignore') 运行出来时成功的,搞定了,谢谢大神

使用道具

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

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-1 16:58