我正在寻找创建一个程序,它将接收任何文件并将文件中的字符分成两个单独的文件,对应于任何一行的左侧和右侧。到目前为止我写过这个但是我已经卡住了。任何adivice将不胜感激
源
$cat > foo
abcdef
ghijkl
期望的结果:
print(foe)
abcghi
print(fum)
defjkl
foo = open(foo)
def write_left(foo):
foo_linse=file_len(foo)
foe = ''
fum = ''
with open(foo) as infile:
for line in infile:
while i <= foo_lines:
x = len(line)
mid = int(x/2) - 1
foe = foe.append(foo[:mid])
fum = fum.append(fo0[mid:])
write_left(foo)
答案 0 :(得分:2)
def left_right(filename):
import re
parsefl = open(str(filename), "r")
left = []
right = []
for line in parsefl:
numRead = int(len(line) / 2)
rightRead = re.sub("\n", "", line[numRead:])
line = re.sub(rightRead, '', line)
leftRead = re.sub("\n", "", line)
right.append(rightRead)
left.append(leftRead)
parsefl.close()
return left, right
在调用函数时指定要分隔的文件名,如:left_right("file_to_parse")
。此函数返回一个列表元组,第一个列表是文件的左侧,第二个列表是文件的右侧。