简体   繁体   中英

Removing the added “\n” parts from a list created from a text file

I read lines from a text file to get list of paths, the txt file sample is:

/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 ,

To read it I'm using:

x = open('/File_list.txt', 'r')
y = [line.split(',') for line in x.readlines()]

But each element have now \\n at the end, for example for y[2] :

['/data0/home/rslat/GFDL/archive/fms/mom4/mom4p0/mom4p0c/mom4_test8/preprocessing/fe_dep_ginoux_gregg_om3_bc.nc=Soluble_Fe_Flux_PI.nc ',
 '\n']

How do I remove these unecessary \\n ? Tried:

good = [line.rstrip('\n') for line in y]

But got the error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-33-d3ed0e6bdc26> in <module>
----> 1 good = [line.rstrip('\n') for line in y]

<ipython-input-33-d3ed0e6bdc26> in <listcomp>(.0)
----> 1 good = [line.rstrip('\n') for line in y]

AttributeError: 'list' object has no attribute 'rstrip'

Seems like a simple issue but I couldn't resolve it yet.

This should help. You can check if the line is empty using if line.strip()

Ex:

with open('/File_list.txt') as infile:
    #good = [line.strip().split(",") for line in infile if line.strip()]
    good = [line.strip(" ,\n") for line in infile if line.strip()]

Apply .strip method before split, it will remove any non-printable character from the end of a line such as \\r, \\n, \\t, etc.

x = open('/File_list.txt', 'r')
y = [line.strip().split(',') for line in x.readlines()]

If the number of lines in the file is small(say less than 1000 or 10000), you can also use .read method instead of .readlines like below:

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

Try this,

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

Output:

['/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']

Two solutions :

Reading all the file :

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

Reading by lines :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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