简体   繁体   English

Python连接mySQL数据库写入文本文件

[英]Python connect mySQL database write to text file

I connect my python to MySQL server with mySQL-Connector(in pyCharm) and i can read from server then write text file.This seems to be: 我用mySQL-Connector(在pyCharm中)将我的python连接到MySQL服务器,我可以从服务器读取然后写入文本文件。这似乎是:

(1, 'PENELOPE', 'GUINESS', datetime.datetime(2006, 2, 15, 4, 34, 33))
(2, 'NICK', 'WAHLBERG', datetime.datetime(2006, 2, 15, 4, 34, 33))
(3, 'ED', 'CHASE', datetime.datetime(2006, 2, 15, 4, 34, 33))
(4, 'JENNIFER', 'DAVIS', datetime.datetime(2006, 2, 15, 4, 34, 33))
(5, 'JOHNNY', 'LOLLOBRIGIDA', datetime.datetime(2006, 2, 15, 4, 34, 33))

I need to change the commas which are between two areas from , to ~ i can find source code could you help me ?Which class I do change? 我需要更改两个区域之间的逗号 〜我可以找到源代码,你可以帮助我吗?我改变哪个班级? this is my python code 这是我的python代码

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="2153417",
  database="sakila"
)
tweets = open("keywords.txt", "w")
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM actor")

with open("C:/Users/Erhan/PycharmProjects/MySQL/keywords.txt", "w", newline='') as f:
    for row in mycursor:
        print(row, file=f)

this is working correctly just need change commas( , ) among name,surname and datetime 这是正常工作只需要在名称,姓氏和日期时间之间更改逗号(

like this 像这样

(1~ 'PENELOPE' ~ 'GUINESS' ~ datetime.datetime(2006, 2, 15, 4, 34, 33)) 
 (2~ 'NICK' ~ 'WAHLBERG' ~ datetime.datetime(2006, 2, 15, 4, 34, 33))

Assuming you want the string representation of all objects(eg 2006-02-15 04:34:33 ) instead of the objects representations datetime.datetime(2006, 2, 15, 4, 34, 33) , try this: 假设你想要所有对象的字符串表示(例如2006-02-15 04:34:33 )而不是对象表示datetime.datetime(2006, 2, 15, 4, 34, 33) 2006-02-15 04:34:33 datetime.datetime(2006, 2, 15, 4, 34, 33) ,试试这个:

As per your requirement, try this: 根据您的要求,试试这个:

print(' ~ '.join(map(repr, row)), file=f)

I would recommend to try directly writing to the file instead: 我建议尝试直接写入文件:

f.write(' ~ '.join(map(repr, row))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM