我尝试按分隔符"}.
分割文件,但是找不到分隔符,结果我只得到一个新文件,其内容与原始文件相同。代码是:
with open('okladki_200_01') as fp:
contents = fp.read()
i = 1
for entry in contents.split('"}.'):
f= open("okladka_%s" % i,"w+")
f.write(entry)
f.close()
i += 1
可以帮忙吗?
编辑: 该文件的内容如下:
{"base64Image":"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB\nAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB\nAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAusFMADASIA\nAhEBAxEB/8QAHwAAAgIBBQEBAAAAAAAAAAAAAgQAAwUBBgcICQoL/8QAaRAAAQEFBAcDBwgHBQYD\nAwEZAwIBBBESEwAhIiMFFDEyM0FDUVNhBiRCY3GBkQcIFTRSc6GxRGKDk8HR8FRyo+HxCRYlZLPD\ndILTFzWEkp [...] 3aIiVoL1pmNQxjWr27\nPBnhatT94NfdwDzDBz9aSP/Z\n","elementHashcode":-1794239528,"imageOrientation":6,"type":"BOOK"}
{"base64Image":"/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB\nAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
我想我刚刚发现了问题... HxD查看器将0x0A ASCII字符显示为点,但这是换行符。所以我应该寻找'"}\n'
答案 0 :(得分:0)
将contents.split('“}。')移至其自己的变量中。
lines = contents.split('.}"')
for entry in lines:
...
代码:
with open('textfile') as fp:
contents = fp.read()
i = 1
lines = contents.split('.}"')
for entry in lines:
f= open("textfile_%s" % i,"w+")
f.write(entry)
f.close()
i += 1
fp.close()
答案 1 :(得分:0)
您是否真的需要检查方括号?在您的情况下,您的输入文件似乎已经被格式化为1个内容= 1行,因此我们的分隔符可以改为\n
,而我们可以使用readlines()
。
这是一个可能的解决方案:
with open('okladki_200_01') as fp:
lines = fp.readlines() # this is a list of strings.
i = 1
for line in lines:
entry = line.lstrip("{").rstrip("}\n") # some clean-up.
f = open("okladka_%s" %i ,"w+")
f.write(entry)
f.close()
i += 1