[英]Run a python function for each line in a text file
好的,所以我正在嘗試批量格式化大型文本文檔以進行轉換
#{'000','001','002','003','004','005','006','007','008','009'}
進入
#{'000':'001','002':'003','004':'005','006':'007','008':'009'}
使用 python 並讓我的 function 工作,但是只有當我逐行運行它時它才會工作。
並且想知道如何讓它在我輸入的每一行上運行,以便它可以在多行文檔上工作
with open("input") as core:
a = core.read()
K = '!'
N = 12
res = ''
for idx, ele in enumerate(a):
if idx % N == 0 and idx != 0:
res = res + K
else:
res = res + ele
b = (str(res).replace(",",":").replace("!",","))
l = len(b)
c = b[:l-1]
d = c + "}"
print(d)
這是多行文本文件的當前結果
{'000':'001','002':'003','004':'005','006':'007','008':'009',
{'001':'00,':'003':'00,':'005':'00,':'007':'00,':'009':'00,'}
{'002':',03':'004':',05':'006':',07':'008':',09':'000':',01'}
{'003','004':'005','006':'007','008':'009','000':'001','002'}
到目前為止我已經嘗試過
with open('input', "r") as a:
for line in a:
K = '!'
N = 12
res = ''
for idx, ele in enumerate(a):
if idx % N == 0 and idx != 0:
res = res + K
else:
res = res + ele
b = (str(res))
l = len(b)
c = b[:l-1]
d = c + "}"
print(d)
但沒有運氣
找到解決方案
import re
with open("input") as core:
coords = core.read()
sword = coords.replace("\n",",\n")
dung = re.sub('(,[^,]*),', r'\1 ', sword).replace(",",":").replace(" ",",").replace(",\n","\n")
print(dung)
我知道我的解決方案有效,但我不能將其完全應用於我根據需要應用不同格式的其他情況。 由於那里有太多文檔,因此很容易弄清楚如何格式化單行文本。
有誰知道任何插件或特定的 python 元素,您可以在其中編寫格式 function,然后將其應用於所有行。 就像一種 applylines() 擴展而不是 readlines()
你可以這樣做:
# Read in the file
with open('input.txt', 'r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace(',', ':')
# Write the file out again
with open('output.txt', 'w') as file:
file.write(filedata)
您可以將每行的文件內容分開,然后在每行上應用文本處理函數。 之后只是 append 響應 output 的行。代碼將是
with open("input") as core:
a = core.read()
K = '!'
N = 12
a = a.split("\n")
res = ''
for line in a:
temp = ''
for idx, ele in enumerate(line):
if idx % N == 0 and idx != 0:
temp = temp + K
else:
temp = temp + ele
temp = (str(temp).replace(",",":").replace("!",","))
res = res+temp[:-1]+"}\n"
res = res[:-1]
print(res)
對於以下輸入
{'000','001','002','003','004','005','006','007','008','009'}
{'000','001','002','003','004','005','006','007','008','009'}
output 將是:
{'000':'001','002':'003','004':'005','006':'007','008':'009'}
{'000':'001','002':'003','004':'005','006':'007','008':'009'}
我想我的答案是將您的輸入數據轉換為生成器,這樣我就可以將next()
應用於它以一次獲取兩個項目。
def clean_line(line):
items = iter(line.split(","))
return ','.join(f'{item}:{next(items)}' for item in items)
使用像clean_line()
這樣的方法,您現在可以使用:
data = [
"{'000','001','002','003','004','005','006','007','008','009'}",
"{'000','001','002','003','004','005','006','007','008','009'}"
]
results = "\n".join(clean_line(line) for line in data)
print(results)
或者從文件中讀取為:
def clean_line(line):
items = iter(line.strip("\n").split(","))
return ','.join(f'{item}:{next(items)}' for item in items)
with open("data.txt", "r") as file_in:
results = "\n".join(clean_line(line) for line in file_in.readlines())
print(results)
對於給定的示例輸入,您可以使用.read()
一次讀取整個文件,使用模式匹配第一個逗號,並捕獲第 1 組匹配直到第二個逗號。
在替換中使用:
並使用\1
對第 1 組中捕獲的內容進行反向引用
,([^,\n]*,)?
部分中的模式匹配:
,
匹配一個逗號(
捕獲組 1
[^,\n]*,
可選擇匹配除逗號或換行符外的任意字符,然后匹配一個逗號)?
關閉捕獲組並使其可選查看正則表達式演示
例如:
import re
with open("input") as core:
dung = re.sub(r",([^,\n]*,)?", r":\1", core.read())
print(dung)
輸出
#{'000':'001','002':'003','004':'005','006':'007','008':'009'}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.