简体   繁体   English

如何在 python 列表中的每个 URL 的开头/结尾添加引号?

[英]How can I add quotes to the beginning/end of every URL in a python list?

Simple inquiry here that I can't seem to figure out.这里的简单查询,我似乎无法弄清楚。 I've written a line of code to add commas to the end of a list of several hundred URLs as so:我已经编写了一行代码,将逗号添加到数百个 URL 列表的末尾,如下所示:

with open('bdall.txt') as f:
    lines = f.read().splitlines()
    new_line = ', '.join(lines)

But how do I modify this (or write new code) to also put all of the URLs from that text file into quotes so that I can paste them all into a program I am writing?但是如何修改这个(或编写新代码)以将该文本文件中的所有 URL 放入引号中,以便我可以将它们全部粘贴到我正在编写的程序中?

The output of the above is:上面的output是:

http://www.metrolyrics.com/til-i-fell-in-love-with-you-lyrics-bob-dylan.html,
http://www.metrolyrics.com/i-heard-that-lonesome-whistle-lyrics-bob-dylan.html,
etc.

But I need但是我需要

"http://www.metrolyrics.com/til-i-fell-in-love-with-you-lyrics-bob-dylan.html", "http://www.metrolyrics.com/i-heard-that-lonesome-whistle-lyrics-bob-dylan.html",
etc.

Instead of directly passing "lines" as parameter for the ".join()" method, you can turn it into a list comprehension where you just need to format each line like this:无需直接将“lines”作为“.join()”方法的参数传递,您可以将其转换为列表理解,您只需像这样格式化每一行:

with open('bdall.txt') as f:
    lines = f.read().splitlines()
    new_line = ', '.join([f'"{line}"' for line in lines])

With this solution, the possibilities are endless.有了这个解决方案,可能性是无穷无尽的。 I hope that was helpful我希望这有帮助

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

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