简体   繁体   English

如何使用 python 在列表中逐行插入字符串

[英]how to insert string line by line in list with python

i want to run the code in every line in this string to get only the value of the driver (c,d,e) and then but them in list the string我想在此字符串的每一行中运行代码以仅获取驱动程序的值(c,d,e),然后将它们列在字符串中

1                C           1048576                  30 GB IFS                
2                d           1048576                  30 GB IFS  
1                e           1048576                  30 GB IFS 

i use that code but i repeat every value of (c,d,e) 3 times in the list我使用该代码,但我在列表中重复 (c,d,e) 的每个值 3 次

d[]
for line in data.split('\n')[1:]:
                    dliver = ouput2[17]
                    print('dliver',dliver)
                    dliver1 = dliver+':'
                    print(dliver1)
                    d.append(dliver1)
                    print('d',d)

note data the veritable inside it the value of string注意数据里面的名副其实的字符串的值

In this line在这一行

for line in data.split('\n')[1:]:

you get a variable called line that you can work with.你会得到一个名为line的变量,你可以使用它。 But you never do.但你永远不会。

If I understand the question correctly, you simply want如果我正确理解了这个问题,您只是想要

dliver = line[17]

instead of代替

dliver = ouput2[17]

At first we create a data structure(list) named output .首先,我们创建一个名为output的数据结构(列表)。 Then we iterate over the string.然后我们遍历字符串。 As the last step we are splitting it with spaces and newlines in the list comprehension.作为最后一步,我们在列表理解中用空格和换行符将其拆分。 At the last we append the second string of the list into the output list.最后我们将 append 列表的第二个字符串放入output列表中。

output = []
for i in stringer:
    splitted = [x for x in i.split() if x != '']
    output.append(splitted[2])

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

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