CSV是一种以逗号分隔数值的文件类型,在数据库或电子表格中,常见的导入导出文件格式就是CSV格式,CSV格式存储数据通常以纯文本的方式存数数据表。
csv文件
1,2,3,4,5,6,7,8,9,10
10,9,8,7,6,5,4,3,2,1
使用python的csv模块读写csv文件
import csv
fileName = "test.csv"
with open(fileName, "r", encoding="utf-8") as f:
text = csv.reader(f)
for line in text:
for i in line:
print(i)
#输出
1
2
3
4
5
6
7
8
9
10
10
9
8
7
6
5
4
3
2
1
1 | 2 | 3 | 4 |
---|---|---|---|
asd | asdf | sadfsda | asd |
sad | ds | f | f |
- xlrd主要是用来读取excel文件
- xlwt主要是用来写excel文件
import xlrd
data = xlrd.open_workbook("test.xlsx")
table = data.sheets()[0]
rows = table.nrows
cols = table.ncols
print(cols)
for i in range(rows):
print(table.row_values(i))
print("##"*10)
for j in range(cols):
print(table.col_values(j))
print("###"*10)
for row in range(rows):
for col in range(cols):
cell = table.cell_value(row, col)
print(cell)
#输出
4
['one', 'two', 'three', 'four']
['asd', 'asdf', 'sadfsda', 'asd']
['sad', 'ds', 'f', 'f']
####################
['one', 'asd', 'sad']
['two', 'asdf', 'ds']
['three', 'sadfsda', 'f']
['four', 'asd', 'f']
##############################
one
two
three
four
asd
asdf
sadfsda
asd
sad
ds
f
f
# 写文件
import xlwt
workbook = xlwt.Workbook()
sheet1 = workbook.add_sheet("test1", cell_overwrite_ok=True)
sheet1.write(0,0,"hello1")
sheet1.write(0,1,"hello2")
sheet1.write(0,2,"hello3")
sheet1.write(1,0,"word1")
sheet1.write(1,1,"word2")
sheet1.write(1,2,"word3")
sheet1.write(1,3,"word4")
workbook.save("testwrite.xls")
print("create ok")
hello1 | hello2 |
word1 | word2 |
- pdf文件操作
pdfkit模块用途
pdfkit是将网页转换成pdf文档的一个模块。
pdfkit模块用于Python中,更具有可操作性,可以将网页中需要的部分转化成pdf文档
pdfkit用法
简单讲,可以有以下三种用法,第一个参数为将要转化的html链接(文件,或字符串),第二个参数为保存在本地的pdf文档名称。
import pdfkit
# pdfkit.from_url("https://www.jd.com", 'jdindex.pdf')
# pdfkit.from_file("test.html", 'test.pdf')
pdfkit.from_string('sasdlfjalsdfjlsda;jfl;sdajfl;asdjflasdjfl;sdajfla;sdjfl;asdjf', 'str.pdf')