Python实验三:Web模块-CSV模块

本次练习-Web-CSV模块

#!/usr/bin/python3
# -*- coding:UTF-8 -*-
import requests;

# 【通过requests进行数据下载】
res = requests.get('http://www.gutenberg.org/cache/epub/1112/pg1112.txt');
# print(res.status_code) #200
# print(requests.codes.ok);#200

# 检测是否下载成功【失败:则抛出异常,成功:没有任何输出】
# res.raise_for_status();
# 获取所有下载的文本内容;
# print(res.text)

# 如果下载的内容已经超出:Unicode编码,则使用二进制文件写入
# 【错误提示】UnicodeEncodeError: 'gbk' codec can't encode character '\ufeff'
# payFile = open('2.txt', 'wb');
# # iter_content()在循环的每次迭代中,返回一段内容
# for chunk in res.iter_content(100000):
#     payFile.write(chunk);
# payFile.close();


# ----------------【暂未实验】
# 【BeautifulSoup测试】【暂未实验】
# import requests,bs4;
# res=requests.get('http://nostarch.com')
# res.raise_for_status();
# noStarchSoup=bs4.BeautifulSoup(res.text);
# type(noStarchSoup);
# from selenium 【控制浏览器】【暂未实验】

# 【PDF文件测试实验】
# import PyPDF2;# 【暂未实验】

# 【docx文档测试】【暂未测试】

# 【CSV测试】
import csv;

# 【CSV读取文件】
# example = open('3.csv');
# exampleRead = csv.reader(example);
# exampleData = list(exampleRead);
# print(exampleData);# 以复合列表的形式显示:[['A],['B']]
# for so in exampleRead:  # exampleRead.line_num 输出行数:1,2,3,4,5...【在跳跃行数的时候,很有用】
#     print(so);#可以使用str(so)转换成适合阅读的形式;

# 【CSV文件写入】
# outPutFile = open('3.csv', 'a', newline='');  # 如果不设置newline=''参数,将会以隔行显示
# outPutWrite = csv.writer(outPutFile, delimiter='\t', lineterminator='\n\n');
# # delimiter='\t', lineterminator='\n\n' 将单元格之间的字符改变为制表符,将行之间的字符改变成两个换行符
# outPutWrite.writerow(['Spam', 'OK', 'Hello', 'World']);
# outPutWrite.writerow(['Cav', 'Tos', 'Worlds', 'Cmos']);
# outPutFile.close();

# 【JSON字符串与Python字符串转换】
# json.load(Json data) #将json数据转换成Python数据值
# json.dumps(Python data)#将Python数据值转换成json数据值

发布了46 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/tangqing24680/article/details/102951980