运行 python 中的程序时如何跳过我的 .txt 文件中的空行
How to skip blank lines in my .txt file while running a program in python
我正在编写一个程序,该程序从文本文件中读取和提取数据,然后对数据进行一些计算。
但是,由于我的文本文件中有空格,检索数字时出错。我得到
ValueError: invalid literal for int() with base10: ' '
我的文本文件看起来像这样:
['flower', 'sdfsd', '21/08/2019 20:27:03', 1]
['hello', 'sdsdfsdf', '21/08/2019 20:27:36', '2']
['car', 'sdfsd', '21/08/2019 20:27:45', 1]
['table', 'sdfsdf', '21/08/2019 20:29:21', 1]
_(blank space \n)_
['shirt', 'sdfsdf', '21/08/2019 20:30:02', '2']
_(blank space \n)_
['hello', 'sdfsdf', '21/08/2019 20:30:07', '3']
我从 python 写入文本文件的代码如下所示:
historyFile = open('history.txt','a')
historyFile.write("\n\r")
historyFile.write(file)
historyFile.close()
我已经在网上试过像if not line.strip():
这样的例子,但是它在空行后停止检索数据
以下是我的代码:
readFile = open('history.txt','r')
searching = readFile.readlines()
i=0
for line in searching:
if i in range(len(searching)-1):
i += 1
oldCount = str(int(searching[i].split(",")[3][-4]))
newCount = str(int(searching[i].split(",")[3][-4]) + 1)
print("\nold count: " + oldCount + " new count: " + newCount)
它应该 运行 通过我的文本文件中的所有行和 return 我列表中第 3 位的数字,但是由于空行,我得到了错误
for line in searching:
if line != '\n': # check if line not '\n'
if i in range(len(searching)-1):
....
如果您的 history.txt
包含(注意空白行):
flower, 'sdfsd', '21/08/2019 20:27:03', 1
hello, 'sdsdfsdf', '21/08/2019 20:27:36', 2
car, 'sdfsd', '21/08/2019 20:27:45', 1
table, 'sdfsdf', '21/08/2019 20:29:21', 1
shirt, 'sdfsdf', '21/08/2019 20:30:02', 2
hello, 'sdfsdf', '21/08/2019 20:30:07', 3
然后此脚本将读取文件并打印最后一列增加 1:
with open('history.txt', 'r') as readFile:
for i, line in enumerate(readFile, 1):
line = line.strip()
if not line:
continue
line_tuple = tuple(map(str.strip, line.split(',')))
oldCount = int(line_tuple[3])
newCount = oldCount + 1
print("row no. {} old count: {} new count: {}".format(i, oldCount, newCount))
打印:
row no. 1 old count: 1 new count: 2
row no. 2 old count: 2 new count: 3
row no. 3 old count: 1 new count: 2
row no. 4 old count: 1 new count: 2
row no. 6 old count: 2 new count: 3
row no. 8 old count: 3 new count: 4
编辑:如果您的 history.txt
包含带引号的字段:
'flower','sdfsd','21/08/2019 20:27:03','1'
'hello','sdsdfsdf','21/08/2019 20:27:36','2'
'car','sdfsd','21/08/2019 20:27:45','1'
'table','sdfsdf','21/08/2019 20:29:21','1'
'shirt','sdfsdf','21/08/2019 20:30:02','2'
'hello','sdfsdf','21/08/2019 20:30:07','3'
可以使用csv
模块阅读:
import csv
with open('history.txt', 'r') as readFile:
csvreader = csv.reader(readFile, delimiter=',', quotechar="'")
for i, line in enumerate(csvreader, 1):
if not line:
continue
oldCount = int(line[3])
newCount = oldCount + 1
print("row no. {} old count: {} new count: {}".format(i, oldCount, newCount))
我正在编写一个程序,该程序从文本文件中读取和提取数据,然后对数据进行一些计算。
但是,由于我的文本文件中有空格,检索数字时出错。我得到
ValueError: invalid literal for int() with base10: ' '
我的文本文件看起来像这样:
['flower', 'sdfsd', '21/08/2019 20:27:03', 1]
['hello', 'sdsdfsdf', '21/08/2019 20:27:36', '2']
['car', 'sdfsd', '21/08/2019 20:27:45', 1]
['table', 'sdfsdf', '21/08/2019 20:29:21', 1]
_(blank space \n)_
['shirt', 'sdfsdf', '21/08/2019 20:30:02', '2']
_(blank space \n)_
['hello', 'sdfsdf', '21/08/2019 20:30:07', '3']
我从 python 写入文本文件的代码如下所示:
historyFile = open('history.txt','a')
historyFile.write("\n\r")
historyFile.write(file)
historyFile.close()
我已经在网上试过像if not line.strip():
这样的例子,但是它在空行后停止检索数据
以下是我的代码:
readFile = open('history.txt','r')
searching = readFile.readlines()
i=0
for line in searching:
if i in range(len(searching)-1):
i += 1
oldCount = str(int(searching[i].split(",")[3][-4]))
newCount = str(int(searching[i].split(",")[3][-4]) + 1)
print("\nold count: " + oldCount + " new count: " + newCount)
它应该 运行 通过我的文本文件中的所有行和 return 我列表中第 3 位的数字,但是由于空行,我得到了错误
for line in searching:
if line != '\n': # check if line not '\n'
if i in range(len(searching)-1):
....
如果您的 history.txt
包含(注意空白行):
flower, 'sdfsd', '21/08/2019 20:27:03', 1
hello, 'sdsdfsdf', '21/08/2019 20:27:36', 2
car, 'sdfsd', '21/08/2019 20:27:45', 1
table, 'sdfsdf', '21/08/2019 20:29:21', 1
shirt, 'sdfsdf', '21/08/2019 20:30:02', 2
hello, 'sdfsdf', '21/08/2019 20:30:07', 3
然后此脚本将读取文件并打印最后一列增加 1:
with open('history.txt', 'r') as readFile:
for i, line in enumerate(readFile, 1):
line = line.strip()
if not line:
continue
line_tuple = tuple(map(str.strip, line.split(',')))
oldCount = int(line_tuple[3])
newCount = oldCount + 1
print("row no. {} old count: {} new count: {}".format(i, oldCount, newCount))
打印:
row no. 1 old count: 1 new count: 2
row no. 2 old count: 2 new count: 3
row no. 3 old count: 1 new count: 2
row no. 4 old count: 1 new count: 2
row no. 6 old count: 2 new count: 3
row no. 8 old count: 3 new count: 4
编辑:如果您的 history.txt
包含带引号的字段:
'flower','sdfsd','21/08/2019 20:27:03','1'
'hello','sdsdfsdf','21/08/2019 20:27:36','2'
'car','sdfsd','21/08/2019 20:27:45','1'
'table','sdfsdf','21/08/2019 20:29:21','1'
'shirt','sdfsdf','21/08/2019 20:30:02','2'
'hello','sdfsdf','21/08/2019 20:30:07','3'
可以使用csv
模块阅读:
import csv
with open('history.txt', 'r') as readFile:
csvreader = csv.reader(readFile, delimiter=',', quotechar="'")
for i, line in enumerate(csvreader, 1):
if not line:
continue
oldCount = int(line[3])
newCount = oldCount + 1
print("row no. {} old count: {} new count: {}".format(i, oldCount, newCount))