Python 从从文本文件创建的列表中删除添加的“\n”部分

Python 从从文本文件创建的列表中删除添加的“\n”部分,python,list,Python,List,我从文本文件中读取行以获得路径列表,txt文件示例为: /data0/home/rslat/GFDL/archive/edg/fms/river_routes_gt74Sto61S=river_destination_field , /data0/home/rslat/GFDL/archive/fms/mom4/mom4p1/mom4p1a/mom4_ecosystem/preprocessing/rho0_profile.nc , /data0/home/rslat/GFDL/archive/

我从文本文件中读取行以获得路径列表,txt文件示例为:

/data0/home/rslat/GFDL/archive/edg/fms/river_routes_gt74Sto61S=river_destination_field ,
/data0/home/rslat/GFDL/archive/fms/mom4/mom4p1/mom4p1a/mom4_ecosystem/preprocessing/rho0_profile.nc ,
/data0/home/rslat/GFDL/archive/fms/mom4/mom4p0/mom4p0c/mom4_test8/preprocessing/fe_dep_ginoux_gregg_om3_bc.nc=Soluble_Fe_Flux_PI.nc ,
/data0/home/rslat/GFDL/archive/jwd/regression_data/esm2.1/input/cover_type_1860_g_ens=cover_type_field ,
要阅读它,我使用:

x = open('/File_list.txt', 'r')
y = [line.split(',') for line in x.readlines()]
但是现在每个元素的结尾都有\n,例如y[2]:

如何删除这些不必要的\n? 尝试:

但我得到了一个错误:

-------------------------------------- AttributeError回溯最近一次呼叫last 在里面 -->1 good=[line.rstrip'\n'表示y中的行] in.0 -->1 good=[line.rstrip'\n'表示y中的行] AttributeError:“list”对象没有属性“rstrip” 这似乎是一个简单的问题,但我还不能解决它。

这应该会有帮助。可以使用if line.strip检查该行是否为空

例:

应用.strip方法在拆分之前,它将从行的末尾删除任何不可打印的字符,例如\r\n\t等

x = open('/File_list.txt', 'r')
y = [line.strip().split(',') for line in x.readlines()]
如果文件中的行数小于1000或10000,也可以使用.read方法而不是.readlines,如下所示:

x = open('/File_list.txt', 'r')
y = x.read().strip().split('\n')
试试这个

with open("/File_list.txt", "r") as f:
    data = [line.replace("\n","").strip(",").strip() for line in f.readlines()]
输出:

两种解决方案:

正在读取所有文件:

x = open('/File_list.txt', 'r')
fileData = x.read()
y = fileData.split(',')
// can use y.pop() to remove the last empty element if existing
逐行阅读:

x = open('/File_list.txt', 'r')
y = [line[:-1] for line in x.readlines()]
// just remove the last ',' on the line, don't need to split

假设您的文件类似于:path1、\n path2、\n path3,您可能不需要使用“,”进行拆分,因为每一行都已经是要附加到列表中的项目。我尝试了,但由于某些原因,我最后仍然得到了“”,我需要元素y[2]要成为“/data0/home/rslat/GFDL/archive/fms/mom4/mom4p0/mom4p0c/mom4\u test8/preprocessing/fe\u dep\u ginoux\u gregg\u om3\u bc.nc=solible\u fe\u Flux\u PI.nc”有可能吗?你能发布你的文本样本吗?和预期的itLooks输出一样,您需要good=[line.strip.strip.strip for line in infle if line.strip]?或者good=[line.strip,\n对于infle if line.strip中的line]?感谢@Itamar Mushkin更正我的格式。不客气,这是这里的常见做法,无需担心:-
with open("/File_list.txt", "r") as f:
    data = [line.replace("\n","").strip(",").strip() for line in f.readlines()]
['/data0/home/rslat/GFDL/archive/edg/fms/river_routes_gt74Sto61S=river_destination_field',
 '/data0/home/rslat/GFDL/archive/fms/mom4/mom4p1/mom4p1a/mom4_ecosystem/preprocessing/rho0_profile.nc',
 '/data0/home/rslat/GFDL/archive/fms/mom4/mom4p0/mom4p0c/mom4_test8/preprocessing/fe_dep_ginoux_gregg_om3_bc.nc=Soluble_Fe_Flux_PI.nc',
 '/data0/home/rslat/GFDL/archive/jwd/regression_data/esm2.1/input/cover_type_1860_g_ens=cover_type_field']
x = open('/File_list.txt', 'r')
fileData = x.read()
y = fileData.split(',')
// can use y.pop() to remove the last empty element if existing
x = open('/File_list.txt', 'r')
y = [line[:-1] for line in x.readlines()]
// just remove the last ',' on the line, don't need to split