MySQL:使用“ SELECT INTO OUTFILE”

时间:2019-05-11 13:13:23

标签: python mysql database mysql-workbench select-into-outfile

我使用以下命令从数据库中导出了两个字段:nameheader

SELECT name, header 
INTO OUTFILE '/var/lib/mysql-files/myfile.txt'
FIELDS TERMINATED BY '<xx>' 
LINES TERMINATED BY '\n'
FROM mytable;

一条记录具有此header值:

  

{'Date':'Fri,19 Apr 2019 07:23:14 GMT','Server':'Apache','Vary':   “质量扫描”,“严格运输安全”:   'max-age = 31536000; includeSubDomains; preload','Set-Cookie':   'ASP.NET_SessionId = ivoa5bhet0s2ygkylmimvkie;路径= /;安全;   HttpOnly; SameSite = strict,   SC_ANALYTICS_GLOBAL_COOKIE = 12f133ea5080403692b4ce458fd1a540;   expires =星期四,2029年4月19日7:23:14 GMT;路径= /;安全;   HttpOnly; SameSite = strict,   SC_ANALYTICS_SESSION_COOKIE = 336B597E7A534D6393C57DF11E047484 | 1 | ivoa5bhet0s2ygkylmimvkie;   路径= /;安全; HttpOnly; SameSite = strict,   incap_ses_885_270026 = cDp / VlO1AHgshF9F6SZIDGJ3uVwAAAAAg7DwpecyehBCyhXgoYO5GA ==;   路径= /; Domain = .zurich.co.uk,___ utmvmykuNyVY = dlNaoEsuXSO;路径= /;   Max-Age = 900,___ utmvaykuNyVY = nWJx01KvGT;路径= /;最高年龄= 900,   ___ utmvbykuNyVY = JZy       XEtOwalQ:PtR;路径= /; Max-Age = 900','X-Content-Type-Options':'nosniff','X-XSS-Protection':'1; mode = block','Cache-Control':   'private','Content-Type':'text / html; charset = utf-8',“保持活动”:   'timeout = 5,max = 10','Connection':'Keep-Alive','X-Iinfo':   '8-3925806-3925807 NNNN CT(73 151 0)RT(1555658593583 5)q(0 0 3 0)   r(6 6)U5”,“ X-CDN”:“ Incapsula”,“ Content-Encoding”:“ gzip”,   'Transfer-Encoding':'chunked'}

它导出为:

  

https://z.co.uk <xx> {'Date':'Fri,19 Apr 2019 07:23:14 GMT',   '服务器':'Apache','Vary':'Qualys-Scan',   “严格运输安全”:   'max-age = 31536000; includeSubDomains; preload','Set-Cookie':   'ASP.NET_SessionId = ivoa5bhet0s2ygkylmimvkie;路径= /;安全;   HttpOnly; SameSite = strict,   SC_ANALYTICS_GLOBAL_COOKIE = 12f133ea5080403692b4ce458fd1a540;   expires =星期四,2029年4月19日7:23:14 GMT;路径= /;安全;   HttpOnly; SameSite = strict,   SC_ANALYTICS_SESSION_COOKIE = 336B597E7A534D6393C57DF11E047484 | 1 | ivoa5bhet0s2ygkylmimvkie;   路径= /;安全; HttpOnly; SameSite = strict,   incap_ses_885_270026 = cDp / VlO1AHgshF9F6SZIDGJ3uVwAAAAAg7DwpecyehBCyhXgoYO5GA ==;   路径= /; Domain = .zurich.co.uk,___ utmvmykuNyVY = dlNaoEsuXSO;路径= /;   Max-Age = 900,__ utmvaykuNyVY = nWJx01KvGT;路径= /;最高年龄= 900,   ___ utmvbykuNyVY = JZy

然后在新行中(请注意,它以一个标签开头,这就是堆栈溢出将其显示为代码的原因):

XEtOwalQ: PtR; path=/; Max-Age=900', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1; mode=block', 'Cache-Control':
     

'private','Content-Type':'text / html; charset = utf-8',“保持活动”:   'timeout = 5,max = 10','Connection':'Keep-Alive','X-Iinfo':   '8-3925806-3925807 NNNN CT(73 151 0)RT(1555658593583 5)q(0 0 3 0)   r(6 6)U5”,“ X-CDN”:“ Incapsula”,“ Content-Encoding”:“ gzip”,   'Transfer-Encoding':'chunked'}

为什么会这样?如何避免这种情况?

这给我带来了一个大问题,并出现在许多其他记录中(但不是全部)。

我需要使用python读取文件行,并且python将切碎的行标识为两行而不是一行,这使得这些行不符合我使用python处理的行格式,并且出现错误提示{{1 }}。

1 个答案:

答案 0 :(得分:0)

类似这样的东西(未经测试):

with open('/var/lib/mysql-files/myfile.txt') as f:
    lines = f.read().splitlines()
i = 0
lines2 = []
while i<len(lines):
    if ('{' in lines[i]) and ('}' not in lines[i]):
        l = lines[i] + ' ' + lines[i+1] + '\n'
        i += 1
    else:
        l = lines[i] + '\n'
    lines2.append(l)
    i += 1

with open('/var/lib/mysql-files/fixed.txt', 'w') as f:
    f.writelines(lines2)