简体   繁体   English

如何将纯文本列表转换为 python 3 列表

[英]How can I convert a plain text list to a python 3 list

I would like to take a list of names and put them into a python list with the correct syntax.我想使用正确的语法将名称列表放入 python 列表中。 How could I do this?我怎么能这样做? Because otherwise I would have to go through and put in quotes and commas everywhere and I am dealing with long lists.因为否则我将不得不通过 go 并在任何地方加上引号和逗号,我正在处理长列表。

For example if I had:例如,如果我有:

list = [
name
name1
name2
name3
name4
name5
]

How could I change those names to strings without doing each one individually?我怎样才能将这些名称更改为字符串而不单独执行每个名称?

You can use.split() and split on the newline characters ( \n ).您可以使用.split()并拆分换行符( \n )。 split() will return an array of names: split()将返回一个名称数组:

>>> x = """name
... name1
... name2
... name3
... name4
... name5
... """

>>> names = x.split('\n')
>>> print(names)
['name', 'name1', 'name2', 'name3', 'name4', 'name5', '']

1- read the plain text from a file then split by \n 1- 从文件中读取纯文本,然后由 \n 分割

2- use this website: https://delim.co/# in the converter settings you could add quotes 2-使用此网站: https://delim.co/#在转换器设置中,您可以添加引号

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

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