[英]txt file sorting(key:value in every line) - a problem with '\n'
我正在嘗試對看起來像這樣的 txt 文件進行排序:
byr:1983 iyr:2017 pid:796082981 cid:129 eyr:2030 ecl:oth hgt:182cm iyr:2019 cid:314 eyr:2039 hcl:#cfa07d hgt:171cm ecl:#0180ce byr:2006 pid:8204115568 byr:1991 eyr:2022 hcl:#341e13 iyr:2016 pid:729933757 hgt:167cm ecl:gry hcl:231d64 cid:124 ecl:gmt eyr:2039 hgt:189in pid:#9c3ea1
依此類推(+1000 行),到該結構:
byr:value iyr:value eyr:value hgt:value hcl:value ecl:value pid:value cid:value byr:value iyr:value eyr:value hgt:value hcl:value ecl:value pid:value cid:value
byr、iyr 等順序無關緊要,但 key:value 的每個“集合”都必須用空行分隔。 我的主要問題,如果我可以這樣稱呼它,是創建一段代碼,當有多個 key:value 元素時對文件進行正確排序,我設法取得了一些進展,但它仍然不是應該的- 以下代碼:
result_file = open('testresult.txt', 'w')
#list_of_lines = [] testing purpose
with open('input.txt', 'r') as f:
for line in f:
if line == "\n":
#list_of_lines.append('\n') testing
result_file.writelines('\n')
else:
for i in line.split(' '):
if i[-1] == "n":
result_file.write(i)
else:
result_file.write(i + '\n')
#print(i) testing purpose
正在制作如下結果:
byr:1983
iyr:2017
pid:796082981
cid:129
eyr:2030
ecl:oth
hgt:182cm
iyr:2019
cid:314
eyr:2039
hcl:#cfa07d
hgt:171cm
ecl:#0180ce
byr:2006
pid:8204115568
byr:1991
eyr:2022
hcl:#341e13
iyr:2016
pid:729933757
hgt:167cm
ecl:gry
如您所見,它無法正常工作 - 例如,在第一次出現 byr 和第一次出現 hgt 之間不應該有空行,依此類推。 在我看來,最后一個 if 語句
if i[-1] == "n":
result_file.write(i)
else:
result_file.write(i + '\n')
正在保護我免受這種情況的影響,但現在我完全不明白為什么它不像我“預測”的那樣。 請幫忙。 感謝提前<3
嘗試這個 -
result_file = open('testresult.txt', 'w')
#list_of_lines = [] testing purpose
with open('input.txt', 'r') as f:
for line in f:
if line == '\n':
#list_of_lines.append('\n') testing
result_file.writelines('\n')
else:
# replace '\n' with ''
line = line.replace('\n', '')
for i in line.split(' '):
result_file.writelines(i + '\n')
result_file.close()
嘗試這個
lines = []
with open("file.txt", "r") as f:
lines = f.readlines()
print(lines)
splited_lines = []
for line in lines:
[ splited_lines.append(splited) for splited in line.split(" ")]
print("splitted_lines")
print(splited_lines)
# notice every occurence in splitted_lines has a '\n',
# that might be causing your more then on newline problem,
# lets remove that
cleaned_lines = []
[cleaned_lines.append(splited.strip("\n")) for splited in splited_lines]
print("Removed /n")
print(cleaned_lines)
with open("output.txt", "w") as f:
for line in cleaned_lines:
f.write(line+"\n")
在 file.txt 中有這個:
byr:1983 iyr:2017
pid:796082981 cid:129 eyr:2030
ecl:oth hgt:182cm
iyr:2019
cid:314
eyr:2039 hcl:#cfa07d hgt:171cm ecl:#0180ce byr:2006 pid:8204115568
byr:1991 eyr:2022 hcl:#341e13 iyr:2016 pid:729933757 hgt:167cm ecl:gry
hcl:231d64 cid:124 ecl:gmt eyr:2039
hgt:189in
pid:#9c3ea1
運行上面的腳本在 output.txt 中給了我這個:
byr:1983
iyr:2017
pid:796082981
cid:129
eyr:2030
ecl:oth
hgt:182cm
iyr:2019
cid:314
eyr:2039
hcl:#cfa07d
hgt:171cm
ecl:#0180ce
byr:2006
pid:8204115568
byr:1991
eyr:2022
hcl:#341e13
iyr:2016
pid:729933757
hgt:167cm
ecl:gry
hcl:231d64
cid:124
ecl:gmt
eyr:2039
hgt:189in
pid:#9c3ea1
希望這是你需要的嗎?
您可以使用replace刪除所有\n
。
result_file = open('testresult.txt', 'w')
#list_of_lines = [] testing purpose
with open('input.txt', 'r') as f:
for line in f:
line = line.replace('\n', '')
if line != '':
for i in line.split(' '):
result_file.write(i+'\n')
這是結果:
byr:1983
iyr:2017
pid:796082981
cid:129
eyr:2030
ecl:oth
hgt:182cm
iyr:2019
cid:314
eyr:2039
hcl:#cfa07d
hgt:171cm
ecl:#0180ce
byr:2006
pid:8204115568
byr:1991
eyr:2022
hcl:#341e13
iyr:2016
pid:729933757
hgt:167cm
ecl:gry
hcl:231d64
cid:124
ecl:gmt
eyr:2039
hgt:189in
pid:#9c3ea1
正則表達式可能有助於實現您的結果,而不會因行尾字符而煩惱。
假設您的配對中沒有空格,您可以使用以下腳本:
import re
from contextlib import ExitStack
REGEX = re.compile(r"[^:\s]+:\S+")
with ExitStack() as stack:
fr = stack.enter_context(open(input, encoding="UTF_8"))
fw = stack.enter_context(open(output, mode="w", encoding="UTF_8"))
for line in fr:
match = REGEX.match(line)
if not match:
fw.write("\n")
continue
for item in REGEX.findall(line):
fw.write(f"{item}\n")
正則表達式可幫助您搜索“任何不是分號或空白字符的內容,后跟一個分號。然后是任何不是空白字符的內容”。 這允許腳本只關注對。
空白字符包括空格、制表符和行尾字符。
ExitStack 功能有助於優化兩個上下文管理器的使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.