首页 技术 正文
技术 2022年11月20日
0 收藏 537 点赞 4,692 浏览 2069 个字

概述

  Python在处理CSV文件时,如果writerow的对象是<type ‘unicode’>字符串时,写入到CSV文件时将会出现一个字符占一个单元格的情况:

python在处理CSV文件时,字符串和列表写入的区别

  

  但是将字符串转换为列表类型时,进行writerow写入即可实现,writerow一个列表只占一个单元格:

python在处理CSV文件时,字符串和列表写入的区别

代码:

#encoding=utf-8
#不打开浏览器进行操作
import sys,csv,codecs
import time
from lxml import etree
reload(sys)
sys.setdefaultencoding('utf-8')
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument("headless")
driver = webdriver.Chrome(chrome_options=option)
# driver = webdriver.Chrome()
driver.get("https://www.kanzhun.com/xs/?ka=head-salary")
print([driver.title])
#打开CSV文件
csvfile=codecs.open('C:\Users\Desktop\test.csv','wb','gbk')
writer = csv.writer(csvfile)
writer.writerow(driver.title)
#获取城市工资排名
aa=driver.find_elements_by_xpath('/html/body/div/section[1]/div[3]/dl[1]/dd/ul/li/i')
bb=driver.find_elements_by_xpath('/html/body/div/section[1]/div[3]/dl[1]/dd/ul/li/a')
cc=driver.find_elements_by_xpath('/html/body/div/section[1]/div[3]/dl[1]/dd/ul/li/span')
print('======地区工资排行(TOP10)======')
writer.writerow([u'======地区工资排行(TOP10)======'])
for (i,o,p) in zip(aa,bb,cc):
i = i.text
o = o.text
p = p.text
print(type(p))
ss = [i, o, p]
writer.writerow(ss)
print(i+'...'+o+'...'+p)
#获取公司工资排名
aa=driver.find_elements_by_xpath('/html/body/div/section[1]/div[3]/dl[2]/dd/ul/li/i')
bb=driver.find_elements_by_xpath('/html/body/div/section[1]/div[3]/dl[2]/dd/ul/li/a')
cc=driver.find_elements_by_xpath('/html/body/div/section[1]/div[3]/dl[2]/dd/ul/li/span')
print('======公司工资排行(TOP10)======')
writer.writerow([u'======公司工资排行(TOP10)======'])
for (i,o,p) in zip(aa,bb,cc):
i = i.text
o = o.text
p = p.text
ss = [i, o, p]
writer.writerow(ss)
print(i+'...'+o+'...'+p)
#获取职位工资排名
aa=driver.find_elements_by_xpath('/html/body/div/section[1]/div[3]/dl[3]/dd/ul/li/i')
bb=driver.find_elements_by_xpath('/html/body/div/section[1]/div[3]/dl[3]/dd/ul/li/a')
cc=driver.find_elements_by_xpath('/html/body/div/section[1]/div[3]/dl[3]/dd/ul/li/span')
print('======职位工资排行(TOP10)======')
writer.writerow([u'======职位工资排行(TOP10)======'])
for (i,o,p) in zip(aa,bb,cc):
i = i.text
o = o.text
p = p.text
ss = [i,o,p]
writer.writerow(ss)
print(i+'...'+o+'...'+p)
csvfile.close()
driver.quit()

  

CSV文件:

python在处理CSV文件时,字符串和列表写入的区别

注意:

其中开头的reload(sys)和打开csv文件时的‘gbk’都是辅助解决字符编码问题的。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,028
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,518
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,365
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,146
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,780
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,857