繁体   English   中英

将文件拆分为列表后删除空格

[英]Remove the white spaces after the splitting the file in to list

for line_1 in file_1 :
    split_op_1 = line_1.split(',')
    print(split_op_1)

输入: 547727, 547728, 553876, 557264,

输出: '547727', ' 547728', ' 553876', ' 557264',

所需输出: '547727', '547728', '553876', '557264',

如何从每个列表元素的开头删除多余的空间?

尝试:

split_op_1 = [i.strip() for i in split_op_1.split(',')]

尝试这个

for line_1 in file_1 :
    split_op_1 = line_1.split(',')
    print(list(map(str.strip, split_op_1)))

strip()函数在应用于字符串时删除前导和尾随空格:

https://docs.python.org/3/library/stdtypes.html#string-methods

暂无
暂无

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

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