【问题标题】:How do you make a change to every line of code in python如何对python中的每一行代码进行更改
【发布时间】:2018-11-27 18:41:42
【问题描述】:

我想在每一行后面加上引号和逗号

apple
pear
squid

变成:

      "apple",
      "pear",
      "squid",

但是如果有无限多的话。

【问题讨论】:

  • 将它们放入列表中。迭代并做出改变。到目前为止,您尝试过任何 python 代码吗?
  • 您确定要在 Python 中执行此操作,还是关于您的编辑器的问题?
  • 使用文本编辑器或命令行可能会更好。 UNIX (Linux, MacOSX, ...) 可以使用sed 命令来做到这一点。
  • apple pearsquid 是什么?变量?字符串?您的问题需要更详细地说明问题的背景以及您想要的结果是什么。

标签: python arrays list


【解决方案1】:

如果您希望以不同的格式打印这些字符串,您可以这样做:

strings = ['apple','pear','squid']

for item in strings:
  print '"{}",'.format(item)

这会导致

"apple",
"pear",
"squid",

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多