Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This is python code, query the table pop form mariadb and writing the three columns of each row to a file in one line with comma separated:
Code:
cur = conn.cursor()
query = "SELECT user, passwd, server FROM pop"
cur.execute(query)
for idx, row in enumerate(cur, start=1):
with open(f'mail{idx}.txt', 'w') as f:
f.write(','.join(row))
I want to write the each row to file in three lines like this:
Distribution: Ubuntu, mainly. Too much stuff works out of the box O.o
Posts: 71
Rep:
Quote:
Originally Posted by shams
This is python code, query the table pop form mariadb and writing the three columns of each row to a file in one line with comma separated:
Code:
cur = conn.cursor()
query = "SELECT user, passwd, server FROM pop"
cur.execute(query)
for idx, row in enumerate(cur, start=1):
with open(f'mail{idx}.txt', 'w') as f:
f.write(','.join(row))
I want to write the each row to file in three lines like this:
Code:
User= user
pssword= passwd
server= server
I'm not familiar with MariaDB, but why not just wrap the file write around the db access and write each iteration? That is to say:
Code:
with open(f'mail{idx}.txt', 'w') as f:
for idx, row in enumerate(cur, start=1):
f.write(row + '\n')
EDIT:
I don't think this is what you want, but your post is a little confusing. If you want 3 separate text files, each containing a comma separated list, looks like you have it.
If you want one text file where each row of the file contains the row of the db, then that's what I put above. I don't understand how the bottom piece correlates to what you're asking.
Maybe you meant you want a separate file for each db row where each row in the file is a col of the db like you showed at the bottom? If that's the case. But I have no idea what this has to do with the comma separated list you mentioned:
Code:
for idx, row in enumerate(cur, start=1):
with open(f'mail{idx}.txt', 'w') as f:
f.write('User={0}\nPassword={1}\nServer={2}\n'.format(row[0], row[1], row[2]))
# Alternatively argument to write can be: f'User={row[0]}\nPassword={row[1]}\nServer={row[2]}\n' - but I prefer the former, just preference.
for idx, row in enumerate(cur, start=1):
with open(f'mail{idx}.txt', 'w') as f:
f.write(f'User= {row[0]}\n')
f.write(f'pssword= {row[1]}\n')
f.write(f'server= {row[2]}\n')
cur = conn.cursor()
query = "SELECT user, passwd, server FROM pop"
cur.execute(query)
for idx, row in enumerate(cur, start=1):
with open(f'mail{idx}.txt', 'w') as f:
f.write('User= {}\npssword= {}\nserver= {}\n'.format(*row))
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.