mysql txt表格格式转换_TXT,xls,mysql之间数据格式转换,新手上路

[Patch] 纯文本查看 复制代码import pymysql,xlwt,xlrd

#我只写了简单的四行数据

#f.writelines('''

#name,age,sex,分数

#Tom,18,男,90

#john,19,女,80

# hi,20,男,90''')

[mw_shl_code=python,true]import pymysql,xlwt,xlrd

def txt_excel():#首先最简单的是txt_excel 我用的是先读出一个列表,遍历列表得出每一行的数据是个字符串,

#然后用spilt以逗号分割成一个新列表,把每个数据都加到相应的表格里

f = open('a.txt', 'r', encoding='utf-8')

f1 = f.readlines()

t = xlwt.Workbook(encoding='utf-8')

sheet = t.add_sheet('phthon1', cell_overwrite_ok=True)

for i in range(len(f1)):

# print(f1[i])

if ',' in f1[i]:

ii=f1[i].split(',')

for iii in range(len(ii)):

sheet.write(i,iii,ii[iii])

# print(iii)

else:

sheet.write(i, 0, f1[i])

t.save('测试1.xls')

f.close()

# txt_excel()

def excel_txt(): #这个就简单了 依据统计的行数来做循环,取出每行的数据,用join粘接起来{:1_927:}

f=open('b.txt','w',encoding='utf-8')

t=xlrd.open_workbook('测试1.xls')

sheet =t.sheets()[0] # 下标位置选择

for i in range(sheet.nrows): #统计行数做循环

tt=sheet.row_values(i) #读出每行的数据

b=','.join(tt) #把读出的列表直接粘接成字符串

f.write(b) #写入

f.close() #关闭

# excel_txt()

def txt_mysql(): #把txt 转mysql,这里的汉字字段名为啥不能直接加,气死,各位大佬指点指点 求助?1

f=open('a.txt','r',encoding='utf-8')

f1=f.readlines()

conn=pymysql.connect(host='192.168.10.182',port=3306,

user='root',passwd='123456',charset='utf8') #连接数据库

ttt=conn.cursor() #创建游标

ttt.execute('use zuoye4;') #进入某一个数据库

ttt.execute('create table xxx(name varchar(20),age int,sex varchar(20))' ) # 创建数据库 我一开始想的是循环加入,

#是我天真了,数据之间的格式不一样,不知道咋办,只能自己加了求助?2

conn.commit() #提交一下操作

ttt.execute('alter table xxx add 分数 float ' ) #这玩意真的nt

conn.commit()

# ttt.execute('drop table xxx')

# conn.commit()

ttt.execute('show tables;')

print(ttt.fetchall())

for i in range(1,len(f1)): #直接从第二行读,第一行是字段名

c=f1[i].replace('\n','') 删除字符串的小尾巴\n

d=c.split(',') #分割成一个列表

ttt.execute(f"INSERT INTO xxx VALUES('{d[0]}',{d[1]},'{d[2]}',{d[3]})") #这里不同的数据类型里有的数据要引号,有的不用,真变态,

#不过从数据库中转出的数据再加入数据库就没这个烦恼了,不知道为什么,求助?3

conn.commit()

ttt.execute('select * from xxx ')

print(ttt.fetchall())

conn.close()

# txt_mysql()

def mysql_txt():

f=open('c.txt','w',encoding='utf-8')

conn=pymysql.connect(host='192.168.1.190',port=3306,

user='root',passwd='123456',charset='utf8') #连接数据库

ttt=conn.cursor()

ttt.execute('show databases;')

ttt.execute('use zuoye4;')

ttt.execute('show tables;')

# ttt.execute('drop tables xxx')

ttt.execute('desc xxx') #查看表结构,会有个大元组包裹小元组产生,我们要的字段名在小元组的第一个,取下标就行了

i=ttt.fetchall() #用fetchall显示结果 给个变量

f.write(f"{i[0][0]},{i[1][0]},{i[2][0]},{i[3][0]}"+'\n') #加入

ttt.execute('select * from xxx;') #查看表内容 会有个大元组包裹小元组产生

for i,j in enumerate(ttt.fetchall()): #循环取每行数据 这里我偷懒了因为我每行只有四个数据,我没用循环加入

f.write(f'{j[0]},{j[1]},{j[2]},{j[3]}'+'\n') #加入到txt \n换行

f.close()

conn.close()

# mysql_txt()

def mysql_excel(): #类似于上边的

t=xlwt.Workbook(encoding='utf-8')

sheet=t.add_sheet('python1',cell_overwrite_ok=True)

conn=pymysql.connect(port=3306,user='root',host='192.168.10.182',password='123456',charset='utf8')

m=conn.cursor()

m.execute('use zuoye4;')

m.execute('desc xxx')

m1=m.fetchall()

for i in range(len(m1)):

sheet.write(0,i,m1[i][0]) #先把字段名加入excel

m.execute('select * from xxx')

m2=m.fetchall()

# print(m2)

# input(' ')

for i in range(len(m2)): #统记有几行 用作行

for y in range(len(m2[i])): #统计每行有几个数据 用作列

print(m2[i][y])

# input(';')

sheet.write(1+i,y,m2[i][y]) #循环写入每个数据 记得行数要加1 不要覆盖了首行内容

# m.execute('')

# m.execute('')

# sheet.write(0,0,'')

t.save('测试11.xls')

conn.close()

# mysql_excel()

def excel_mysql():

t=xlrd.open_workbook('测试11.xls')

sheet=t.sheets()[0]

conn=pymysql.connect(port=3306,user='root',password='123456',charset='utf8',host='192.168.10.182')

m=conn.cursor()

m.execute('use zuoye4;')

# m.execute('drop table yyy;')

# conn.commit()

tt=sheet.row_values(0)

print(tt[0],tt[1],tt[2],tt[3])

input(';')

m.execute(f'create table yyy ({tt[0]} varchar(10),{tt[1]} int,{tt[2]} varchar(10));')

conn.commit()

m.execute(f'alter table yyy add {tt[3]} float;') #汉字真特殊!

conn.commit()

for i in range(1,sheet.nrows):

tt1=sheet.row_values(i)

tt2=tuple(tt1)

# print(tt2)

# input(';')

m.execute(f'insert into yyy values {tt2};') #这里的数据是我直接从上个mysql到excel转换的拿过来直接用的

#,掏出来一看直接格式就有了,一脸懵,直接加求助?4

conn.commit()

m.execute(f'select * from yyy')

print(m.fetchall())

conn.close()

# excel_mysql()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值