Python sh命令中的变量与从文件中读取的变量不一致
我有一个问题,使用带有git的sh模块获得的字符串与写入文件的字符串不匹配 我得到如下提交字符串Python sh命令中的变量与从文件中读取的变量不一致,python,git,Python,Git,我有一个问题,使用带有git的sh模块获得的字符串与写入文件的字符串不匹配 我得到如下提交字符串 from sh import stat, git repo = git.bake(_cwd=git_dir) current_commit=git.log("-n1", "--format='%T'", git_file) print current_commit '89a848eb9ea98bfd6770301dce848052ec8ef63f' type(current_commit) &l
from sh import stat, git
repo = git.bake(_cwd=git_dir)
current_commit=git.log("-n1", "--format='%T'", git_file)
print current_commit
'89a848eb9ea98bfd6770301dce848052ec8ef63f'
type(current_commit)
<class 'sh.RunningCommand'>
with open(commit_store_file, 'w+') as f:
f.write(str(current_commit))
当前的_commit显示如下
from sh import stat, git
repo = git.bake(_cwd=git_dir)
current_commit=git.log("-n1", "--format='%T'", git_file)
print current_commit
'89a848eb9ea98bfd6770301dce848052ec8ef63f'
type(current_commit)
<class 'sh.RunningCommand'>
with open(commit_store_file, 'w+') as f:
f.write(str(current_commit))
类型如下
from sh import stat, git
repo = git.bake(_cwd=git_dir)
current_commit=git.log("-n1", "--format='%T'", git_file)
print current_commit
'89a848eb9ea98bfd6770301dce848052ec8ef63f'
type(current_commit)
<class 'sh.RunningCommand'>
with open(commit_store_file, 'w+') as f:
f.write(str(current_commit))
当前的_提交被写入一个文件,如下所示
from sh import stat, git
repo = git.bake(_cwd=git_dir)
current_commit=git.log("-n1", "--format='%T'", git_file)
print current_commit
'89a848eb9ea98bfd6770301dce848052ec8ef63f'
type(current_commit)
<class 'sh.RunningCommand'>
with open(commit_store_file, 'w+') as f:
f.write(str(current_commit))
当我用vi打开这个文件时,我看到了以下内容
^[[?1h^[=^M'89a848eb9ea98bfd6770301dce848052ec8ef63f'^[[m
^M^[[K^[[?1l^[>
with open(commit_store_file, 'r+') as f:
stored_commit = f.readline()
我把它和下面的内容一起读了回来
^[[?1h^[=^M'89a848eb9ea98bfd6770301dce848052ec8ef63f'^[[m
^M^[[K^[[?1l^[>
with open(commit_store_file, 'r+') as f:
stored_commit = f.readline()
这里的类型是string
type(stored_commit)
<type 'str'>
如何使用sh检索实际字符串并正确存储以使它们匹配
谢谢这些角色对我来说就像寻呼机一样 尝试在命令前面添加
--无寻呼机:
git --no-pager log -n 1 --format='%T' --color=never
该选项需要在log
命令之前,而不是之后,因此您必须bake
将其放入。获得相同效果的另一种方法是使用GIT_PAGER
环境变量或配置设置
获取机器可读树散列的另一种方法是git cat file-p HEAD
。树将出现在输出的第一行,前缀为“tree”。这些字符在我看来就像寻呼机一样
尝试在命令前面添加--无寻呼机:
git --no-pager log -n 1 --format='%T' --color=never
该选项需要在log
命令之前,而不是之后,因此您必须bake
将其放入。获得相同效果的另一种方法是使用GIT_PAGER
环境变量或配置设置
获取机器可读树散列的另一种方法是git cat file-p HEAD
。树将出现在输出的第一行,前缀为“tree”。我不熟悉sh
,因此我无法帮助解决它,但在我看来,您的str(当前提交)
中包含ansi颜色代码。你可以试着告诉git命令不要使用颜色,看看这个帮助我是否不熟悉sh
,因此我无法帮助解决它,但在我看来,你的str(current\u commit)
中有ansi颜色代码。您可以尝试告诉git命令不要使用颜色,看看这是否有帮助