如何检测在 Python 中打开的文件的每一行中的第一个非空白字符?
How can I detect the first non-whitespace character in each line of a file opened in Python?
我正在尝试编写 Python 代码来检测文件中每一行的第一个非空白字符并检查它是否为“}”。例如,如果我的文件内容是...
a
fox {
}
{ jumped
} up
...我想在第三行和第五行检测“}”,尽管它有两个空格。
我试过这样做,但我卡住了:
full_file = open ("filename", "r")
each_line = full_file.readlines()
for item in each_line
if item[0].find('}') != -1:
# do something, such as print (item)
full_file.close()
非常感谢帮助!
您可以尝试在每一行调用 strip()
,然后只检查第一个字符:
full_file = open ("filename", "r")
each_line = full_file.readlines()
for item in each_line
if item.strip() and item.strip()[0] == '}':
print(item)
full_file.close()
您可以使用此函数获取第一个非空白字符。
这一次只读取一行,因此如果您正在处理一个非常大的文件,它可以为您省去一些麻烦。
def get_first_non_whitespace_char(file_name):
with open(file_name, 'r') as fileobj:
for line in fileobj:
for character in line:
if character != " ":
return character
return None
尝试使用这样的文件:
sample.txt
}hello
使用函数
file_name = "sample.txt"
first_nsp_char = get_first_non_whitespace_char(file_name)
if first_nsp_char != None:
print("First Non space character in file:", first_nsp_char)
print("Character is '}' :", first_nsp_char == '}')
else:
print("No non-whitespace characters found in file:", file_name)
输出:
First Non space character in file: }
Character is '}' : True
我的输入略有不同(sample.txt 末尾有两个空行,'\n'):
a
fox {
}
{ jumped
} up
是:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 10 11:28:29 2021
@author: Pietro
"""
def get_first_non_whitespace_char(file_name):
listz ={}
index = 0
with open(file_name, 'r') as fileobj:
for line in fileobj:
index += 1
#print('line: ',line)
for character in line:
if character != " " and character != '\n':
listz[index] = character
# index += 1
break
else:
pass
return listz
file_name = "sample.txt"
first_nsp_char = get_first_non_whitespace_char(file_name)
for i in first_nsp_char:
print('line # ',i,' first character is : ',first_nsp_char[i])
输出:
line # 1 first character is : a
line # 2 first character is : f
line # 3 first character is : }
line # 4 first character is : {
line # 7 first character is : }
我正在尝试编写 Python 代码来检测文件中每一行的第一个非空白字符并检查它是否为“}”。例如,如果我的文件内容是...
a
fox {
}
{ jumped
} up
...我想在第三行和第五行检测“}”,尽管它有两个空格。
我试过这样做,但我卡住了:
full_file = open ("filename", "r")
each_line = full_file.readlines()
for item in each_line
if item[0].find('}') != -1:
# do something, such as print (item)
full_file.close()
非常感谢帮助!
您可以尝试在每一行调用 strip()
,然后只检查第一个字符:
full_file = open ("filename", "r")
each_line = full_file.readlines()
for item in each_line
if item.strip() and item.strip()[0] == '}':
print(item)
full_file.close()
您可以使用此函数获取第一个非空白字符。
这一次只读取一行,因此如果您正在处理一个非常大的文件,它可以为您省去一些麻烦。
def get_first_non_whitespace_char(file_name):
with open(file_name, 'r') as fileobj:
for line in fileobj:
for character in line:
if character != " ":
return character
return None
尝试使用这样的文件:
sample.txt
}hello
使用函数
file_name = "sample.txt"
first_nsp_char = get_first_non_whitespace_char(file_name)
if first_nsp_char != None:
print("First Non space character in file:", first_nsp_char)
print("Character is '}' :", first_nsp_char == '}')
else:
print("No non-whitespace characters found in file:", file_name)
输出:
First Non space character in file: }
Character is '}' : True
我的输入略有不同(sample.txt 末尾有两个空行,'\n'):
a
fox {
}
{ jumped
} up
是:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 10 11:28:29 2021
@author: Pietro
"""
def get_first_non_whitespace_char(file_name):
listz ={}
index = 0
with open(file_name, 'r') as fileobj:
for line in fileobj:
index += 1
#print('line: ',line)
for character in line:
if character != " " and character != '\n':
listz[index] = character
# index += 1
break
else:
pass
return listz
file_name = "sample.txt"
first_nsp_char = get_first_non_whitespace_char(file_name)
for i in first_nsp_char:
print('line # ',i,' first character is : ',first_nsp_char[i])
输出:
line # 1 first character is : a
line # 2 first character is : f
line # 3 first character is : }
line # 4 first character is : {
line # 7 first character is : }