Apache Camel不会在源文件夹中保留文件(Apache Camel does not retain file in source folder)
在使用Apache Camel时,无论何时使用camel-zip压缩文件或通过canel-ftp复制文件,Camel都会将文件移动到.Camel文件夹中,并且在处理后不会将其保留在源文件夹中。 我希望我的文件在处理完成后留在源文件夹中。 请指教如何实现同样的目标。
CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder(){
@Override public void configure() throws Exception { from("file:src/main/zip?delete=false").marshal().zipFile() .to("file:C:/Users/kdewan/Desktop"); } }); context.start(); Thread.sleep(5000); context.stop();
While working on Apache Camel, whenever a file is zipped using camel-zip or copied via canel-ftp, Camel moves the file in .Camel folder and does not retain it in source folder after processing. I want my file to stay in the source folder after the processing has been done. Please advice how to achieve the same.
CamelContext context = new DefaultCamelContext(); context.addRoutes(new RouteBuilder() {
@Override public void configure() throws Exception { from("file:src/main/zip?delete=false").marshal().zipFile() .to("file:C:/Users/kdewan/Desktop"); } }); context.start(); Thread.sleep(5000); context.stop();
原文:https://stackoverflow.com/questions/27539882
最满意答案
这里有一个生成器函数,它充当文件中的迭代器 ,根据所有文件中奇异的换行符进行剪切。
它通过
lenchunk
字符块读取文件,并在每个当前块中显示行,块后显示块。由于换行符是3个字符(':;:'),因此可能会发生块以换行符结束:此生成器函数负责处理这种可能性并设法显示正确的行。
如果换行符只有一个字符,功能可以简化。 我只写了最微妙的情况下的功能。
使用此功能可以一次读取一行文件,而无需将整个文件读入内存。
from random import randrange, choice # this part is to create an exemple file with newline being :;: alphabet = 'abcdefghijklmnopqrstuvwxyz ' ch = ':;:'.join(''.join(choice(alphabet) for nc in xrange(randrange(0,40))) for i in xrange(50)) with open('fofo.txt','wb') as g: g.write(ch) # this generator function is an iterator for a file # if nl receives an argument whose bool is True, # the newlines :;: are returned in the lines def liner(filename,eol,lenchunk,nl=0): # nl = 0 or 1 acts as 0 or 1 in splitlines() L = len(eol) NL = len(eol) if nl else 0 with open(filename,'rb') as f: chunk = f.read(lenchunk) tail = '' while chunk: last = chunk.rfind(eol) if last==-1: kept = chunk newtail = '' else: kept = chunk[0:last+L] # here: L newtail = chunk[last+L:] # here: L chunk = tail + kept tail = newtail x = y = 0 while y+1: y = chunk.find(eol,x) if y+1: yield chunk[x:y+NL] # here: NL else: break x = y+L # here: L chunk = f.read(lenchunk) yield tail for line in liner('fofo.txt',':;:'): print line
这里也是一样,在这里和那里打印以允许遵循算法。
from random import randrange, choice # this part is to create an exemple file with newline being :;: alphabet = 'abcdefghijklmnopqrstuvwxyz ' ch = ':;:'.join(''.join(choice(alphabet) for nc in xrange(randrange(0,40))) for i in xrange(50)) with open('fofo.txt','wb') as g: g.write(ch) # this generator function is an iterator for a file # if nl receives an argument whose bool is True, # the newlines :;: are returned in the lines def liner(filename,eol,lenchunk,nl=0): L = len(eol) NL = len(eol) if nl else 0 with open(filename,'rb') as f: ch = f.read() the_end = '\n\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'+\ '\nend of the file=='+ch[-50:]+\ '\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n' f.seek(0,0) chunk = f.read(lenchunk) tail = '' while chunk: if (chunk[-1]==':' and chunk[-3:]!=':;:') or chunk[-2:]==':;': wr = [' ##########---------- cut newline cut ----------##########'+\ '\nchunk== '+chunk+\ '\n---------------------------------------------------'] else: wr = ['chunk== '+chunk+\ '\n---------------------------------------------------'] last = chunk.rfind(eol) if last==-1: kept = chunk newtail = '' else: kept = chunk[0:last+L] # here: L newtail = chunk[last+L:] # here: L wr.append('\nkept== '+kept+\ '\n---------------------------------------------------'+\ '\nnewtail== '+newtail) chunk = tail + kept tail = newtail wr.append('\n---------------------------------------------------'+\ '\ntail + kept== '+chunk+\ '\n---------------------------------------------------') print ''.join(wr) x = y = 0 while y+1: y = chunk.find(eol,x) if y+1: yield chunk[x:y+NL] # here: NL else: break x = y+L # here: L print '\n\n===================================================' chunk = f.read(lenchunk) yield tail print the_end for line in liner('fofo.txt',':;:',1): print 'line== '+line
。
编辑
我比较了我的代码和chmullig代码的执行时间。
用一个大约10 MB的'fofo.txt'文件创建
alphabet = 'abcdefghijklmnopqrstuvwxyz ' ch = ':;:'.join(''.join(choice(alphabet) for nc in xrange(randrange(0,60))) for i in xrange(324000)) with open('fofo.txt','wb') as g: g.write(ch)
并测量这样的时间:
te = clock() for line in liner('fofo.txt',':;:', 65536): pass print clock()-te fh = open('fofo.txt', 'rb') zenBreaker = SpecialDelimiters(fh, ':;:', 65536) te = clock() for line in zenBreaker: pass print clock()-te
我获得了几篇论文中观察到的以下最小时间:
............我的代码0,7067秒
chmullig的代码为0.8373秒
。
编辑2
我改变了我的生成器函数:
liner2()
采用文件处理程序而不是文件名。 所以文件的打开可能会被忽略,因为它是用来测量chmullig的代码的def liner2(fh,eol,lenchunk,nl=0): L = len(eol) NL = len(eol) if nl else 0 chunk = fh.read(lenchunk) tail = '' while chunk: last = chunk.rfind(eol) if last==-1: kept = chunk newtail = '' else: kept = chunk[0:last+L] # here: L newtail = chunk[last+L:] # here: L chunk = tail + kept tail = newtail x = y = 0 while y+1: y = chunk.find(eol,x) if y+1: yield chunk[x:y+NL] # here: NL else: break x = y+L # here: L chunk = fh.read(lenchunk) yield tail fh = open('fofo.txt', 'rb') te = clock() for line in liner2(fh,':;:', 65536): pass print clock()-te
在看过最短时间的众多散文后,结果是
.........与班轮()0.7067秒
.......与liner2()0.7064秒
chmullig的代码为0.8373秒
事实上,文件的打开在整个时间内都是无穷小的部分。
Here's a generator function thats acts as an iterator on a file, cuting the lines according exotic newline being identical in all the file.
It reads the file by chunks of
lenchunk
characters and displays the lines in each current chunk, chunk after chunk.Since the newline is 3 characters in my exemple (':;:'), it may happen that a chunk ends with a cut newline: this generator function takes care of this possibility and manages to display the correct lines.
In case of a newline being only one character, the function could be simplified. I wrote only the function for the most delicate case.
Employing this function allows to read a file one line at a time, without reading the entire file into memory.
from random import randrange, choice # this part is to create an exemple file with newline being :;: alphabet = 'abcdefghijklmnopqrstuvwxyz ' ch = ':;:'.join(''.join(choice(alphabet) for nc in xrange(randrange(0,40))) for i in xrange(50)) with open('fofo.txt','wb') as g: g.write(ch) # this generator function is an iterator for a file # if nl receives an argument whose bool is True, # the newlines :;: are returned in the lines def liner(filename,eol,lenchunk,nl=0): # nl = 0 or 1 acts as 0 or 1 in splitlines() L = len(eol) NL = len(eol) if nl else 0 with open(filename,'rb') as f: chunk = f.read(lenchunk) tail = '' while chunk: last = chunk.rfind(eol) if last==-1: kept = chunk newtail = '' else: kept = chunk[0:last+L] # here: L newtail = chunk[last+L:] # here: L chunk = tail + kept tail = newtail x = y = 0 while y+1: y = chunk.find(eol,x) if y+1: yield chunk[x:y+NL] # here: NL else: break x = y+L # here: L chunk = f.read(lenchunk) yield tail for line in liner('fofo.txt',':;:'): print line
Here's the same, with printings here and there to allow to follow the algorithm.
from random import randrange, choice # this part is to create an exemple file with newline being :;: alphabet = 'abcdefghijklmnopqrstuvwxyz ' ch = ':;:'.join(''.join(choice(alphabet) for nc in xrange(randrange(0,40))) for i in xrange(50)) with open('fofo.txt','wb') as g: g.write(ch) # this generator function is an iterator for a file # if nl receives an argument whose bool is True, # the newlines :;: are returned in the lines def liner(filename,eol,lenchunk,nl=0): L = len(eol) NL = len(eol) if nl else 0 with open(filename,'rb') as f: ch = f.read() the_end = '\n\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'+\ '\nend of the file=='+ch[-50:]+\ '\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n' f.seek(0,0) chunk = f.read(lenchunk) tail = '' while chunk: if (chunk[-1]==':' and chunk[-3:]!=':;:') or chunk[-2:]==':;': wr = [' ##########---------- cut newline cut ----------##########'+\ '\nchunk== '+chunk+\ '\n---------------------------------------------------'] else: wr = ['chunk== '+chunk+\ '\n---------------------------------------------------'] last = chunk.rfind(eol) if last==-1: kept = chunk newtail = '' else: kept = chunk[0:last+L] # here: L newtail = chunk[last+L:] # here: L wr.append('\nkept== '+kept+\ '\n---------------------------------------------------'+\ '\nnewtail== '+newtail) chunk = tail + kept tail = newtail wr.append('\n---------------------------------------------------'+\ '\ntail + kept== '+chunk+\ '\n---------------------------------------------------') print ''.join(wr) x = y = 0 while y+1: y = chunk.find(eol,x) if y+1: yield chunk[x:y+NL] # here: NL else: break x = y+L # here: L print '\n\n===================================================' chunk = f.read(lenchunk) yield tail print the_end for line in liner('fofo.txt',':;:',1): print 'line== '+line
.
EDIT
I compared the times of execution of my code and of the chmullig's code.
With a 'fofo.txt' file about 10 MB, created with
alphabet = 'abcdefghijklmnopqrstuvwxyz ' ch = ':;:'.join(''.join(choice(alphabet) for nc in xrange(randrange(0,60))) for i in xrange(324000)) with open('fofo.txt','wb') as g: g.write(ch)
and measuring times like that:
te = clock() for line in liner('fofo.txt',':;:', 65536): pass print clock()-te fh = open('fofo.txt', 'rb') zenBreaker = SpecialDelimiters(fh, ':;:', 65536) te = clock() for line in zenBreaker: pass print clock()-te
I obtained the following minimum times observed on several essays:
............my code 0,7067 seconds
chmullig's code 0.8373 seconds
.
EDIT 2
I changed my generator function:
liner2()
takes a file-handler instead of the file's name. So the opening of the file can be put out of the measuring of time, as it is for the measuring of chmullig's codedef liner2(fh,eol,lenchunk,nl=0): L = len(eol) NL = len(eol) if nl else 0 chunk = fh.read(lenchunk) tail = '' while chunk: last = chunk.rfind(eol) if last==-1: kept = chunk newtail = '' else: kept = chunk[0:last+L] # here: L newtail = chunk[last+L:] # here: L chunk = tail + kept tail = newtail x = y = 0 while y+1: y = chunk.find(eol,x) if y+1: yield chunk[x:y+NL] # here: NL else: break x = y+L # here: L chunk = fh.read(lenchunk) yield tail fh = open('fofo.txt', 'rb') te = clock() for line in liner2(fh,':;:', 65536): pass print clock()-te
The results, after numerous essays to see the minimum times, are
.........with liner() 0.7067seconds
.......with liner2() 0.7064 seconds
chmullig's code 0.8373 seconds
In fact the opening of the file counts for an infinitesimal part in the total time.
相关问答
更多-
如何使用python从文本文件中的行读取特定字符?(How to read specific characters from lines in a text file using python?)[2022-05-21]
我会使用正则表达式,但这里是一个没有的版本,比@Thrustmaster的解决方案imo更清晰。 >>> text = "[class1] 1:-28 9:-315 13:-354227 2:-36.247 17:-342 8:-34 14:-3825" >>> [int(x.split(':')[0]) for x in text.split()[1:]] [1, 9, 13, 2, 17, 8, 14] I would use regex but here is a version without, ... -
如何只使用python读取某个字符串后的文本文件中的行?(How to only read lines in a text file after a certain string using python?)[2024-04-26]
当您到达您想要开始的线路时,再启动另一个循环: for files in filepath: with open(files, 'r') as f: for line in f: if 'Abstract' in line: for line in f: # now you are at the lines you want # do work 一个文件对 ... -
使用Python从文本文件读取的特殊行尾字符/字符串(Special End-line characters/string from lines read from text file, using Python)[2023-01-03]
这里有一个生成器函数,它充当文件中的迭代器 ,根据所有文件中奇异的换行符进行剪切。 它通过lenchunk字符块读取文件,并在每个当前块中显示行,块后显示块。 由于换行符是3个字符(':;:'),因此可能会发生块以换行符结束:此生成器函数负责处理这种可能性并设法显示正确的行。 如果换行符只有一个字符,功能可以简化。 我只写了最微妙的情况下的功能。 使用此功能可以一次读取一行文件,而无需将整个文件读入内存。 from random import randrange, choice # this part ... -
代替 target = [url.strip() for url in f.readlines()] 使用 pairs = [line.strip().split(' | ') for line in f.readlines()] target = [pair[0] for pair in pairs] name = [pair[1] for pair in pairs] Instead of target = [url.strip() for url in f.readlines()] us ...
-
该行将包含密码,因此只需拆分一次并获取第二个元素: In [20]: from simplecrypt import encrypt In [21]: ciph = encrypt('password', "12345678") In [22]: line = "Password: " + ciph In [23]: line Out[23]: 'Password: sc\x00\x01\x0cP\xa1\xee\'$"\xc1\x85\xe0\x04\xd2wg5\x98\xbf\xb4\xd0\x ...
-
在Python中从文本文件中读取包含文本字符串的整行(Reading a whole line containing a text string from a text file in Python)[2023-10-09]
你需要说: d_lines = data.splitlines() for line in d_lines: if current_space in line: print(line) else: print('Not found') 现在你的条件检查当前空间是否在数据中,我认为它始终是。 you need to say: d_lines = data.splitlines() for line in d_lines: if current_space in li ... -
如何用Python 3替换文本文件中的所有字符串实例?(How to replace all instances of a string in text file with Python 3?)[2022-04-24]
尝试这个: # Make sure this is the valid path to your file file = input("Enter a filename: ") remove = input("Enter the string to be removed: ") # Read in the file with open(file, "r") as file: filedata = file.read() # Replace the target string filedata = ... -
Python正则表达式模式只是普通的旧字符串。 将它们存储在文件中应该没有问题。 也许当你使用file.readline()你会看到转义字符,因为你正在查看该行的repr ? 当您实际使用该模式作为正则表达式时,这应该不是问题: import re filename='/tmp/test.txt' with open(filename,'w') as f: f.write(r'\"Hello,\s\"\s*\+\s*\"world!\"') with open(filename,'r') as f ...
-
Python:文本文件在多行中替换不同的字符串怎么样?(Python: text file replace different strings in multiple lines HOW?)[2022-03-29]
如果要替换字符串开头或结尾的字符,请使用strip 。 如果要删除的字符具有任意位置,请使用替换 ,如下所示: line.replace("~","") 。 请注意,与strip不同,您不能在一个replace调用中指定多个字符,但您可以链接它们,如下所示: line.replace("~","").replace(",","").replace("[","") 只是快速模拟可能适用于您的内容: with open("text.txt", 'r') as f: with open("result.t ... -
List
lines = File.ReadLines(filename).Skip(3).Take(3).ToList(); List lines = File.ReadLines(filename).Skip(3).Take(3).ToList();