- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
**我有一个 ctl 文件,包含如下所示的文本
mycnf_001/mycnf_001_001mycnf_001/mycnf_001_002............................................................(一个很长的列表,有 1000 行)
我很努力,但我无法获得我想要的格式
mycnf_001_001 mycnf_001mycnf_001_002 mycnf_001............................................................(一个很长的列表,有 1000 行)
********* 详细的问题描述***************************
当前格式 mycnf_001/mycnf_001_001
期望从 mycnf_001_001 mycnf_001
//////////代码////////////////////
f = open("ms.ctl", "rb")
s = f.readlines()
f.close()
f = open("newms.ctl", "wb")
s.reverse()
for item in s:
print>>f, item
f.close()
当我执行上面的代码时,它只是从下到上颠倒了顺序,我需要的在上面已经清楚地提到了。
最佳答案
让我们假设在读取文件“ms.ctl”后你有类似的值
s="mycnf_001/mycnf_001_001/mycnf_001_002"
现在根据'/'拆分字符串
spliteds = [x for x in s.split('/') if x.strip()]
现在你有了字符串数组。现在尝试从最后访问数组并将其保存在另一个字符串中。
desireds=""
for i in reversed(spliteds):
desireds = desireds + i + " "
现在您可以将此字符串放入任何文件中。
代码:
f = open("ms.ctl", "rb")
s = f.read()
f.close()
N = s.split('\n')
f = open("newms.ctl", "wb")
spliteds = [ x for x in N[0].split('/') if x.strip()]
desireds=""
for i in reversed(spliteds):
desireds = desireds + i + " "
#print desireds
f.write(desireds)
f.close()
您可以使用 print 调试此代码。希望能帮助到你。
UPDATE
如果你想运行多行。
f = open("ms.ctl", "rb")
s = f.read()
f.close()
N = s.split('\n')
lenth = len(N)
f = open("newms.ctl", "wb")
for x in range(0, lenth):
print "We're on time %d" % (x)
spliteds = [ x for x in N[0].split('/') if x.strip()]
desireds=""
for i in reversed(spliteds):
desireds = desireds + i + " "
#print desireds
f.write(desireds)
f.close()
如果你想得到一列的输出。
在上面的代码中,只需将 desireds = desireds + i + ""
中的 ""
替换为 "\n"
即可
f = open("ms.ctl", "rb")
s = f.read()
f.close()
N = s.split('\n')
lenth = len(N)
f = open("newms.ctl", "wb")
for x in range(0, lenth):
print "We're on time %d" % (x)
spliteds = [ x for x in N[0].split('/') if x.strip()]
desireds=""
for i in reversed(spliteds):
desireds = desireds + i + "\n"
#print desireds
f.write(desireds)
f.close()
关于linux - 如何使用python水平反转ctl文件中的文本数据并保存在新文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41889773/
在长达数周的徒劳尝试中想出一个基本的。 PHP 应用程序,我决定也许我会尝试这个 CFML 的东西。 我已经安装了 XAMPP,并认为我只需要在我的系统上安装 Railo。在某一时刻,GUI 安装程序
我正在尝试使用按键更新并关闭弹出窗口。 如果按下按键 - ctl+s = 保存 如果按键 - ctl+x = 退出 $(window).keypress(function(event) { i
我在装有 OS Yosemite 10.10.5 的 Mac 上使用 XAMPP 时遇到问题 我暂时安装了 XAMPP,尝试重新启动它,但是启动 XAMPP 下载附带的 manager-osx 时,我
我用 C: 编写了这个函数 int process_list(node_t *head){ char c; node_t *prev = head; /* Define pointer
我正在尝试使用 v4l2-ctl 录制一个 15 秒的视频,但我对它的参数有点困惑,即使在 Internet 上查找文章之后也是如此。 v4l2-ctl --set-fmt-video=width=9
我这里有下面的文件 load data infile 'emp.csv' into table EMPLOYEES append fields terminated by ',' optionally
假设我运行 v4l2-ctl --stream-mmap=3 --stream-to=/dev/null,如何在不按 Ctrl+C 用于中断,例如来自另一个 shell 控制台? 最佳答案 除非他们添
我有一个在 Android 上工作的 USB 摄像头(通过 Android On-The-Go),我想改变摄像头的曝光。我已经能够通过在 Linux 机器上使用 v4l2-ctl 来实现它,我希望在我
我真的很喜欢 Vim 插入模式下的 CTRL-W 键,但在正常编辑中我也需要像 CTRL-Del 这样的东西。知道这是否存在,或者如何实现它? 另外,对于 CTRL-H 来说类似的事情怎么样?!我的意
所以,我一直在遵循 GitLab 的安装指南(在 Debian Stretch 上),但我总是遇到同样的错误... 链接:https://about.gitlab.com/install/#debia
我正在尝试为亚洲语言开发 Android IME(需要复杂的渲染,例如更改字形形式、重新排序字符顺序等)。在PC中,使用GTK、Pango、Graphite就足够了。在 Android 中,我如何解决
我想从 Oracle 迁移到 MySQL,其中一个重要步骤是替换构建在 Oracle 环境上的实际作业。 基本上,我每天都会收到一些来自另一个 Oracle 环境的“oracle”转储文件(主要是 C
我目前正在撰写硕士论文,面临着在时间逻辑中指定和验证我的方法。 在我的情况下,哪种时序逻辑最适合使用?我真的很想得到一些关于我的方法以及如何继续的反馈 我的模型由参与者组成,这些参与者将同时执行。对于
我正在学习如何在 Cloud9 中使用 mysql,我有一个脚本用于创建默认数据库和表并将示例数据加载到新数据库中。如何使用mysql-ctl工具执行脚本文件? 它可以很好地连接到数据库,我可以毫无问
即使搜索了太多,我也找不到适合我的问题的答案。我正在使用 Spring Tool Suite 并尝试创建一个新服务器,但收到错误消息 The specified server is not valid
这可能是一个迟钝的问题,但是在提交表单时是否有一种优雅的方式来模仿 anchor 标记的行为?我希望我的用户能够在提交表单时按住 Control 键并在新窗口中打开结果。现在我正在使用一种时髦的、不可
我想要完成的事情: 使用用户定义的波斯语字体和大小(CTL 语言)在 python-docx 中创建段落样式 问题: 我可以使用非 CTL 语言(例如英语)执行此操作: from docx impor
我正在尝试使用 C# 构建我自己的安全(非 http)服务器。当然,我使用的是 SslStream 和 AuthenticateAsServer。这就像冠军一样工作,除了我无法弄清楚如何让服务器将证书
我正在尝试创建一个模仿 emacs CTL-K 的 vim 键绑定(bind): If used at the end of a line, it kills the line-ending newl
我正在尝试使用spring-ldap org.springframework.ldap.support.LdapContextSource类访问LDAP。 以下是我的代码。 config file
我是一名优秀的程序员,十分优秀!