我输入,我的文件中有以下几行。
...
VOAUT0000001712_19774.JPG FRYW-1712-19774.jpg eab516afc1aaa10ad23edb5c15ae4ea4 1712 01
VOAUT0000001712_19775.JPG FRYW-1712-19775.jpg 2715ceba8fd5c69b4ca6952e942a1a8a 1712 02
VOAUT0000001712_19776.JPG FRYW-1712-19776.jpg b1a0c4ec6160da3511e23c617517ff6f 1712 03
VOAUT0000001713_19795.JPG FRYW-1713-19795.jpg 56cd173c6e9436b19d39de214669cc53 1713 01
VOAUT0000001713_19796.JPG FRYW-1713-19796.jpg 271aa1b9ef2ac39c502a270c82b31fa3 1713 02
VOAUT0000001713_19797.JPG FRYW-1713-19797.jpg 667732a85660bebec168bc46b884d9b7 1713 03
VOAUT0000001714_19763.JPG FRYW-1714-19763.jpg d37770d6cde5639ce5db4e6a436498a8 1714 01
VOAUT0000001714_19764.JPG FRYW-1714-19764.jpg ce891ca4d4ea59c3a312a468bb0d4408 1714 02
VOAUT0000001714_19765.JPG FRYW-1714-19765.jpg bd7fed521fe3997bf5c879d9d5ce942d 1714 03
...
我想逐行修改我的文件以便拥有此
17124615_01_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1712-19774.jpg eab516afc1aaa10ad23edb5c15ae4ea4
17124615_02_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1712-19775.jpg 2715ceba8fd5c69b4ca6952e942a1a8a
17124615_03_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1712-19776.jpg b1a0c4ec6160da3511e23c617517ff6f
17134615_01_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1713-19795.jpg 56cd173c6e9436b19d39de214669cc53
17134615_02_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1713-19796.jpg 271aa1b9ef2ac39c502a270c82b31fa3
17134615_03_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1713-19797.jpg 667732a85660bebec168bc46b884d9b7
17144615_01_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1714-19763.jpg d37770d6cde5639ce5db4e6a436498a8
17144615_02_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1714-19764.jpg ce891ca4d4ea59c3a312a468bb0d4408
17144615_03_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1714-19765.jpg bd7fed521fe3997bf5c879d9d5ce942d
这是我的代码的开头:
def renameLineByLine():
with open('/opt/data/photos.txt') as f:
for line in f:
newname, file, path, checksum = line.split()
if ..?? :
try:
rename(...???)
except OSError:
logger.error('Got a problem')
但是我没有看到如何用新格式重命名每一行..?
答案 0 :(得分:2)
您需要正确读入文件:跳过空行并拆分正确的值。没有path
您期望的那个,并且您没有考虑最后的两个数字,这对转换至关重要。
在这里,我只是将所有新格式化的行写入新文件photos_new.txt
。我希望这能让你开始。
但是请注意,您的方法名称renameLineByLine
以及try/except
似乎暗示您还要移动/重命名/对照片执行某些操作。如果是这种情况,这个答案是不够的,你应该试着再详细说明一下。
def renameLineByLine():
new_lines = []
path = '/opt/AutoPrivilege/client/photos/'
with open('/opt/data/photos.txt', 'r') as fin, \
open('/opt/data/photos_new.txt', 'w') as fout:
for line in fin:
if len(line) != 1:
newname, file, checksum, no1, no2 = line.split()
fout.write(" ".join([
"{}4615_{}_hd.jpg".format(no1, no2),
path + file, checksum, '\n'])
)
else:
fout.write('\n')
输入:
VOAUT0000001712_19774.JPG FRYW-1712-19774.jpg eab516afc1aaa10ad23edb5c15ae4ea4 1712 01
VOAUT0000001712_19775.JPG FRYW-1712-19775.jpg 2715ceba8fd5c69b4ca6952e942a1a8a 1712 02
VOAUT0000001712_19776.JPG FRYW-1712-19776.jpg b1a0c4ec6160da3511e23c617517ff6f 1712 03
VOAUT0000001713_19795.JPG FRYW-1713-19795.jpg 56cd173c6e9436b19d39de214669cc53 1713 01
VOAUT0000001713_19796.JPG FRYW-1713-19796.jpg 271aa1b9ef2ac39c502a270c82b31fa3 1713 02
VOAUT0000001713_19797.JPG FRYW-1713-19797.jpg 667732a85660bebec168bc46b884d9b7 1713 03
VOAUT0000001714_19763.JPG FRYW-1714-19763.jpg d37770d6cde5639ce5db4e6a436498a8 1714 01
VOAUT0000001714_19764.JPG FRYW-1714-19764.jpg ce891ca4d4ea59c3a312a468bb0d4408 1714 02
VOAUT0000001714_19765.JPG FRYW-1714-19765.jpg bd7fed521fe3997bf5c879d9d5ce942d 1714 03
输出:
17124615_01_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1712-19774.jpg eab516afc1aaa10ad23edb5c15ae4ea4
17124615_02_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1712-19775.jpg 2715ceba8fd5c69b4ca6952e942a1a8a
17124615_03_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1712-19776.jpg b1a0c4ec6160da3511e23c617517ff6f
17134615_01_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1713-19795.jpg 56cd173c6e9436b19d39de214669cc53
17134615_02_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1713-19796.jpg 271aa1b9ef2ac39c502a270c82b31fa3
17134615_03_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1713-19797.jpg 667732a85660bebec168bc46b884d9b7
17144615_01_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1714-19763.jpg d37770d6cde5639ce5db4e6a436498a8
17144615_02_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1714-19764.jpg ce891ca4d4ea59c3a312a468bb0d4408
17144615_03_hd.jpg /opt/AutoPrivilege/client/photos/FRYW-1714-19765.jpg bd7fed521fe3997bf5c879d9d5ce942d
答案 1 :(得分:1)
已提供生成线条所需的所有信息。感谢@SebastianHöffner指出了明显的问题。
out = open('output.txt','w')
for line in open('data.txt'):
if len(line) != 1:
a, b, c, d, e = line.split()
l = d + '4615_'+ e + '_hd.jpg /opt/AutoPrivilege/client/photos/' + b + ' '+ c
out.write(l + '\n')
else:
out.write('\n')
out.close()
答案 2 :(得分:0)
这样的事情:
#include