如何读取 Python 中已排序和设置的文本文件?
How to read a sorted and set text file in Python?
我是 python 的新手,遇到了我正在读取文本文件并删除不需要的内容并使用 sorted-set 写入新文本文件的问题。然后我需要阅读新的文本文件并继续进行下一步编码。但是当我读取文件时,它的读数是从新文本文件中跳过最后 40 行。
请在下面找到我的编码
f3 = open("data/new.txt", "w")
uniqueperm = []
with open("data/read1.txt", "r") as ins:
d = ins.readlines()
for line in d:
#print(line)
if 'blab: name=' and 'blab1' in line:
line = str(line.replace("blab: name= ", "").replace("'", "").replace("\n", ""))
line = str(line.replace("blab1: ", "").replace("'", "").replace("\n", ""))
#print(line)
uniqueperm.append(line)
#print((line))
for pop in (sorted(set(uniqueperm))):
#print(pop)
f3.write(pop + "\n")
packages = os.listdir("./data/")
inss = open("data/new.txt", 'r').read().split("\n")
print(inss)
output inss 从新文本文件中逐行读取,但跳过新文本文件中的最后 40 奇数行。
请有人帮助我解决这个问题。
提前致谢:)
#read1.txt file contains
blab1: cool.add.WEATHER_ALL
blab1: warm:add.WEATHER_EVERYWHERE
blab: name= hello.add.COPY_HI
blab: name= hello.add.ACCESS_HELLO
blab: name= hello.add.ADD_HI
blab: name= hello.add.WRITE_HI
.
.
.
#it also include repeated data as above so i used set() option so the duplicates are removed before writing to new.txt file
#new.txt file contains
hello.add.ACCESS_HELLO
hello.add.ADD_HI
hello.add.COPY_HI
hello.add.WRITE_HI
cool.add.WEATHER_ALL
warm.add.WEATHER_EVERYWHERE
.
.
.
#new.txt file contains all the data from read1.txt
#when i read this new.txt file in terminal the output is
hello.add.ACCESS_HELLO
hello.add.ADD_HI
hello.add.COPY_HI
hello.add.WRITE_HI
cool.add.WEATHER_ALL
#remaining its skipping or not reading to further proceed
您检查子字符串的条件不正确:
if 'blab: name=' and 'blab1' in line:
将分别检查 'blab: name='
和 'blab1' in line
(条件的 和 部分都需要成立)。你想要的是:
if 'blab: name=' in line or 'blab1' in line:
我是 python 的新手,遇到了我正在读取文本文件并删除不需要的内容并使用 sorted-set 写入新文本文件的问题。然后我需要阅读新的文本文件并继续进行下一步编码。但是当我读取文件时,它的读数是从新文本文件中跳过最后 40 行。
请在下面找到我的编码
f3 = open("data/new.txt", "w")
uniqueperm = []
with open("data/read1.txt", "r") as ins:
d = ins.readlines()
for line in d:
#print(line)
if 'blab: name=' and 'blab1' in line:
line = str(line.replace("blab: name= ", "").replace("'", "").replace("\n", ""))
line = str(line.replace("blab1: ", "").replace("'", "").replace("\n", ""))
#print(line)
uniqueperm.append(line)
#print((line))
for pop in (sorted(set(uniqueperm))):
#print(pop)
f3.write(pop + "\n")
packages = os.listdir("./data/")
inss = open("data/new.txt", 'r').read().split("\n")
print(inss)
output inss 从新文本文件中逐行读取,但跳过新文本文件中的最后 40 奇数行。 请有人帮助我解决这个问题。 提前致谢:)
#read1.txt file contains
blab1: cool.add.WEATHER_ALL
blab1: warm:add.WEATHER_EVERYWHERE
blab: name= hello.add.COPY_HI
blab: name= hello.add.ACCESS_HELLO
blab: name= hello.add.ADD_HI
blab: name= hello.add.WRITE_HI
.
.
.
#it also include repeated data as above so i used set() option so the duplicates are removed before writing to new.txt file
#new.txt file contains
hello.add.ACCESS_HELLO
hello.add.ADD_HI
hello.add.COPY_HI
hello.add.WRITE_HI
cool.add.WEATHER_ALL
warm.add.WEATHER_EVERYWHERE
.
.
.
#new.txt file contains all the data from read1.txt
#when i read this new.txt file in terminal the output is
hello.add.ACCESS_HELLO
hello.add.ADD_HI
hello.add.COPY_HI
hello.add.WRITE_HI
cool.add.WEATHER_ALL
#remaining its skipping or not reading to further proceed
您检查子字符串的条件不正确:
if 'blab: name=' and 'blab1' in line:
将分别检查 'blab: name='
和 'blab1' in line
(条件的 和 部分都需要成立)。你想要的是:
if 'blab: name=' in line or 'blab1' in line: