Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中读取文件,以\n分隔,但忽略最后一个\n_Python_Bash_For Loop_Newline - Fatal编程技术网

在python中读取文件,以\n分隔,但忽略最后一个\n

在python中读取文件,以\n分隔,但忽略最后一个\n,python,bash,for-loop,newline,Python,Bash,For Loop,Newline,我有一个名为list.txt的文件,看起来是这样的: input1 input2 input3 我确信最后一行(输入3)后没有空行。然后我有一个python脚本,它将逐行读取此文件,并将文本写入更多文本中,以创建3个文件(每行一个): 我的问题是,这会创建4个文件,而不是3个:script\u input1.sh、script\u input.sh、script\u input3.sh和script\u.sh。最后一个文件没有其他文件具有input1、input2或input3的文本 Pyth

我有一个名为list.txt的文件,看起来是这样的:

input1
input2
input3
我确信最后一行(输入3)后没有空行。然后我有一个python脚本,它将逐行读取此文件,并将文本写入更多文本中,以创建3个文件(每行一个):

我的问题是,这会创建4个文件,而不是3个:script\u input1.sh、script\u input.sh、script\u input3.sh和script\u.sh。最后一个文件没有其他文件具有input1、input2或input3的文本


Python似乎逐行读取my list.txt,但当它到达“input3”时,它不知怎么会继续?如何让Python逐行读取我的文件,以“\n”分隔,但在最后一个文本后停止?

使用当前的方法,您需要:

  • 检查
    行中的最后一个元素是否为空(
    行[-1]='
  • 如果是,则丢弃它(
    line=line[:-1]
以open('list.txt','r')作为f的
:
lines=f.read().split('\n')
如果行[-1]='':
行=行[:-1]
对于行中的行:
打印(行)
不要忘记,文件不以换行结尾(结尾有空行)是合法的。。。这将处理这种情况


或者,正如@setsquare指出的,您可能希望尝试使用
readlines()

以open('list.txt',r')作为f的
:
lines=[line.rstrip('\n'),用于f.readlines()中的行]
对于行中的行:
打印(行)

使用当前方法,您需要:

  • 检查
    行中的最后一个元素是否为空(
    行[-1]='
  • 如果是,则丢弃它(
    line=line[:-1]
以open('list.txt','r')作为f的
:
lines=f.read().split('\n')
如果行[-1]='':
行=行[:-1]
对于行中的行:
打印(行)
不要忘记,文件不以换行结尾(结尾有空行)是合法的。。。这将处理这种情况


或者,正如@setsquare指出的,您可能希望尝试使用
readlines()

以open('list.txt',r')作为f的
:
lines=[line.rstrip('\n'),用于f.readlines()中的行]
对于行中的行:
打印(行)

您是否考虑过使用readlines()而不是read()?这将让Python为您处理最后一行是否有\n字符的问题

请记住,如果输入文件的最后一行有一个\n,则使用read()并按“\n”拆分将创建一个额外的值。例如:

my_string = 'one\ntwo\nthree\n'
my_list = my_string.split('\n')
print my_list
# >> ['one', 'two', 'three', '']
势解

lines = f.readlines()
# remove newlines
lines = [line.strip() for line in lines]
# remove any empty values, just in case
lines = filter(bool, lines)

有关一个简单的示例,请参见此处:

您是否考虑过使用readlines()而不是read()?这将让Python为您处理最后一行是否有\n字符的问题

请记住,如果输入文件的最后一行有一个\n,则使用read()并按“\n”拆分将创建一个额外的值。例如:

my_string = 'one\ntwo\nthree\n'
my_list = my_string.split('\n')
print my_list
# >> ['one', 'two', 'three', '']
势解

lines = f.readlines()
# remove newlines
lines = [line.strip() for line in lines]
# remove any empty values, just in case
lines = filter(bool, lines)

有关一个简单的示例,请参见此处:

首先,不要在没有太多文件的情况下将整个文件读入内存-文件是可编辑的,因此逐行读取文件的正确方法是:

with open("/path/to/file.ext") as f:
    for line in f:
        do_something_with(line)
现在在for循环中,您只需剥离该行,如果该行为空,则忽略它:

with open("/path/to/file.ext") as f:
    for line in f:
        line = line.strip()
        if not line:
            continue
        do_something_with(line)
稍微不相关,但Python有多行字符串,因此也不需要连接:

# not sure I got it right actually ;)
script_tpl = """
#!/bin/bash 
#BSUB -J {line}.sh 
#BSUB -o /scratch/DBC/user/{line}.sh.out 
#BSUB -e /scratch/DBC/user/{line}.sh.err 
#BSUB -n 1 
#BSUB -q normal 
#BSUB -P DBCDOBZAK 
#BSUB -W 168:00
cd /scratch/DBC/user
grep "input" {line} > result.{line}.txt
"""

with open("/path/to/file.ext") as f:
    for line in f:
        line = line.strip()
        if not line:
            continue
        script = script_tpl.format(line=line)
        with open('script_{}.sh'.format(line), 'w') as output:
            output.write(script)

最后一点注意:避免更改脚本中的目录,请使用
os.path.join()
来处理绝对路径。

首先,不要在没有太多文件的情况下将整个文件读入内存-文件是可移植的,因此逐行读取文件的正确方法是:

with open("/path/to/file.ext") as f:
    for line in f:
        do_something_with(line)
现在在for循环中,您只需剥离该行,如果该行为空,则忽略它:

with open("/path/to/file.ext") as f:
    for line in f:
        line = line.strip()
        if not line:
            continue
        do_something_with(line)
稍微不相关,但Python有多行字符串,因此也不需要连接:

# not sure I got it right actually ;)
script_tpl = """
#!/bin/bash 
#BSUB -J {line}.sh 
#BSUB -o /scratch/DBC/user/{line}.sh.out 
#BSUB -e /scratch/DBC/user/{line}.sh.err 
#BSUB -n 1 
#BSUB -q normal 
#BSUB -P DBCDOBZAK 
#BSUB -W 168:00
cd /scratch/DBC/user
grep "input" {line} > result.{line}.txt
"""

with open("/path/to/file.ext") as f:
    for line in f:
        line = line.strip()
        if not line:
            continue
        script = script_tpl.format(line=line)
        with open('script_{}.sh'.format(line), 'w') as output:
            output.write(script)

最后一点注意:避免在脚本中更改dir,请使用
os.path.join()
来处理绝对路径。

我认为您使用split是错误的

如果您有以下情况:

text = 'xxx yyy'
text.split(' ') # or simply text.split()
结果将是

['xxx', 'yyy']
['xxx', 'yyy', '']
现在,如果您有:

text = 'xxx yyy ' # extra space at the end
text.split()
结果将是

['xxx', 'yyy']
['xxx', 'yyy', '']
,因为split获取每个“”之前和之后的内容(空格)。在本例中,最后一个空格后有空字符串

您可能使用的一些功能:

strip([chars]) # This removes all chars at the beggining or end of a string
例如:

text = '___text_about_something___'
text.strip('_')
结果将是:

'text_about_something'
在您的特定问题中,您可以简单地:

lines = f.readlines() # read all lines of the file without '\n'
for l in lines:
    l.strip(' ') # remove extra spaces at the start or end of line if you need

我认为你用错了split

如果您有以下情况:

text = 'xxx yyy'
text.split(' ') # or simply text.split()
结果将是

['xxx', 'yyy']
['xxx', 'yyy', '']
现在,如果您有:

text = 'xxx yyy ' # extra space at the end
text.split()
结果将是

['xxx', 'yyy']
['xxx', 'yyy', '']
,因为split获取每个“”之前和之后的内容(空格)。在本例中,最后一个空格后有空字符串

您可能使用的一些功能:

strip([chars]) # This removes all chars at the beggining or end of a string
例如:

text = '___text_about_something___'
text.strip('_')
结果将是:

'text_about_something'
在您的特定问题中,您可以简单地:

lines = f.readlines() # read all lines of the file without '\n'
for l in lines:
    l.strip(' ') # remove extra spaces at the start or end of line if you need
f.read()
返回一个以换行符结尾的字符串,它将
split
尽职尽责地视为将最后一行与空字符串分开。不清楚为什么要将整个文件显式地读入内存;只需迭代文件对象,让它处理行分割

with open('list.txt','r') as f:
    for l in f:
        # ...
f.read()
返回一个以换行符结尾的字符串,它将
split
尽职尽责地视为将最后一行与空字符串分开。不清楚为什么要将整个文件显式地读入内存;只需迭代文件对象,让它处理行分割

with open('list.txt','r') as f:
    for l in f:
        # ...

可能重复的我会说:你可能应该重新考虑你的方法。可能重复的我会说:你可能应该重新考虑你的方法。如果最后有多个空行呢?如果处理空行是一个问题,那么我们有一个不同的问题。。。这将只处理常见的“最后一行空”如果最后有多个空行怎么办?如果处理空行是一个问题,那么我们有一个不同的问题。。。这将处理常见的“最后一行为空”Why use
readlines()