欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python批量读取csv并写入_Python如何批量读取CSV文件中指定信息并写入doc文件命名中?...

发布时间:2024/2/28 python 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python批量读取csv并写入_Python如何批量读取CSV文件中指定信息并写入doc文件命名中?... 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

提供思路自己完成吧

1、用 os.walk 加 if 判断将所有的csv 文件名读到list中

用glob 也可以

import glob

for x in glob.glob('*.csv'):

print(x)

2、用csv 库或 第三方库 panpds 循环处理上面list中的文件

3、用doc 库 写doc 文件。

pip install python-docx

import docx

import pandas as pd

# i am not sure how you are getting your data, but you said it is a

# pandas data frame

df = pd.DataFrame(data)

# open an existing document

doc = docx.Document('./test.docx')

# add a table to the end and create a reference variable

# extra row is so we can add the header row

t = doc.add_table(df.shape[0]+1, df.shape[1])

# add the header rows.

for j in range(df.shape[-1]):

t.cell(0,j).text = df.columns[j]

# add the rest of the data frame

for i in range(df.shape[0]):

for j in range(df.shape[-1]):

t.cell(i+1,j).text = str(df.values[i,j])

# save the doc

doc.save('./test.docx')

总结

以上是生活随笔为你收集整理的python批量读取csv并写入_Python如何批量读取CSV文件中指定信息并写入doc文件命名中?...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。