import urllib
import csv
@manager.command
def list_routes():
for rule in app.url_map.iter_rules():
options = {}
for arg in rule.arguments:
options[arg] = "[{0}]".format(arg)
url = rule.rule
line = urllib.parse.unquote("{}{} ".format(rule.endpoint, url))
with open('urls.cvs', 'a') as out:
spamwriter = csv.writer(out, lineterminator='', dialect='excel')
spamwriter.writerows(line)
spamwriter.writerows('\n')
I need to fill in the csv file so that rule.endpoint and url have separate columns.
Your writerows
parameter just needs to be a list of lists of the values the row should contain. So for example writer.writerows([[1, 2], [3, 4]])
will add two rows where each row contains two values. As you add only one row you can use writerow
:
spamwriter.writerow([rule.endpoint, url])
Here is a mock-up. I'm using BytesIO
so that I don't need to actually write to a file:
import csv
import io
c = io.BytesIO()
w = csv.writer(c, lineterminator="", dialect="excel")
w.writerow(['endpoint', 'url'])
print(c.getvalue()) # prints: endpoint,url
import csv
@manager.command
def list_routes():
with open('urls.csv', 'w') as out:
csv_writer = csv.writer(out, lineterminator='\n')
csv_writer.writerows([[rule.endpoint, rule.rule] for rule in app.url_map.iter_rules()])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.