简体   繁体   English

通过读取文件生成列表

[英]generating list by reading from file

i want to generate a list of server addresses and credentials reading from a file, as a single list splitting from newline in file. 我想生成一个从文件读取的服务器地址和凭据列表,作为从文件中的换行符拆分出来的单个列表。

file is in this format 文件是这种格式

login:username
pass:password
destPath:/directory/subdir/
ip:10.95.64.211
ip:10.95.64.215
ip:10.95.64.212
ip:10.95.64.219
ip:10.95.64.213

output i want is in this manner 我想要的输出是以这种方式

[['login:username', 'pass:password', 'destPath:/directory/subdirectory', 'ip:10.95.64.211;ip:10.95.64.215;ip:10.95.64.212;ip:10.95.64.219;ip:10.95.64.213']]

i tried this 我尝试了这个

with open('file') as f:
credentials = [x.strip().split('\n') for x in f.readlines()]

and this returns lists within list 这将返回列表中的列表

[['login:username'], ['pass:password'], ['destPath:/directory/subdir/'], ['ip:10.95.64.211'], ['ip:10.95.64.215'], ['ip:10.95.64.212'], ['ip:10.95.64.219'], ['ip:10.95.64.213']]

am new to python, how can i split by newline character and create single list. 是python的新手,我如何按换行符拆分并创建单个列表。 thank you in advance 先感谢您

You could do it like this 你可以这样

with open('servers.dat') as f:
    L = [[line.strip() for line in f]]
print(L)

Output 输出量

[['login:username', 'pass:password', 'destPath:/directory/subdir/', 'ip:10.95.64.211', 'ip:10.95.64.215', 'ip:10.95.64.212', 'ip:10.95.64.219', 'ip:10.95.64.213']]

Just use a list comprehension to read the lines. 只需使用列表理解即可阅读各行。 You don't need to split on \\n as the regular file iterator reads line by line. 您不需要在\\n上分割,因为常规文件迭代器逐行读取。 The double list is a bit unconventional, just remove the outer [] if you decide you don't want it. 双重列表有点不合常规,如果您决定不要使用外部[]则将其删除。

I just noticed you wanted the list of ip addresses joined in one string. 我只是注意到您想要将IP地址列表加入一个字符串中。 It's not clear as its off the screen in the question and you make no attempt to do it in your own code. 目前尚不清楚,因为它不在问题的屏幕上,您也没有尝试在自己的代码中进行操作。

To do that read the first three lines individually using next then just join up the remaining lines using ; 要做到这一点阅读单独使用的前三行next那么就join了利用剩余的线路; as your delimiter. 作为分隔符。

def reader(f):
    yield next(f)
    yield next(f)
    yield next(f)
    yield ';'.join(ip.strip() for ip in f)

with open('servers.dat') as f:
    L2 = [[line.strip() for line in reader(f)]]

For which the output is 对于其输出

[['login:username', 'pass:password', 'destPath:/directory/subdir/', 'ip:10.95.64.211;ip:10.95.64.215;ip:10.95.64.212;ip:10.95.64.219;ip:10.95.64.213']]

It does not match your expected output exactly as there is a typo 'destPath:/directory/subdirectory' instead of 'destPath:/directory/subdir' from the data. 它与您的预期输出不完全匹配,因为数据中存在错字'destPath:/directory/subdirectory'而不是'destPath:/directory/subdir'

This should work 这应该工作

arr = []
with open('file') as f:
    for line in f:
        arr.append(line)
return [arr]

You could just treat the file as a list and iterate through it with a for loop: 您可以将文件视为列表,然后使用for循环对其进行遍历:

arr = []
with open('file', 'r') as f:
    for line in f:
        arr.append(line.strip('\n'))

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

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