[英]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.