jython 中的数据提取和操作
对于给定的文件
,例如:11,345,sdfsfsfs,1232,
我需要从文件中读取上述记录,读取 11 来分隔符并去除空格并存储在另一个文件中,类似地 345 来分隔符去除空格并存储在文件中。这样我需要对多行进行操作。
所以最后在另一个文件中数据应该看起来像
11,345,sdfsfsfs,1232
请建议我方法。感谢您的帮助。
For a given file
For ex : 11 ,345 , sdfsfsfs , 1232
i need to such above records from a file , read 11 to delimiter and strip the white space and store in the another file , similarly 345 to delimiter strip the while space and store in the file. This way i need to do for multiple rows.
so finally in the other file the data should look like
11,345,sdfsfsfs,1232
Please suggest me the way. Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
打开输入文件(1)和输出文件(2)分别进行读取和写入。
迭代输入文件,获取每一行。用逗号分隔该行。然后使用逗号重新连接该行,但首先删除空格(使用列表理解)。
最后,关闭输入和输出文件。
我不确定 jython 在生成器方面的功能,因此这就是我使用列表理解的原因。请随意编辑它(更了解 jython 的人)。
Open the input file (1) and the output file (2) for reading and writing respectively.
Iterate over the input file, getting each line. Split the line on a comma. Then re-join the line using a comma, but first stripping the whitespace (using a list comprehension).
Finally, close the input and output files.
I'm not sure of jython's capabilities when it comes to generators, so that is why I used a list comprehension. Please feel free to edit this (someone who knows jython better).
您可以采取的一种方法是使用
字符串删除所有空格。翻译
函数。One approach you could take would be to remove all whitespace using the
string.translate
function.