【发布时间】:2021-10-13 06:24:37
【问题描述】:
我正在尝试在 rpy2 脚本中调用 python 字符串变量。该变量应该具有“01”的形状。
我遇到了以下行为:
import rpy2.robjects as robjects
string_to_pass = "01"
robjects.r('''print(paste("Output from R with format and paste:",{}))'''.format(string_to_pass))
将给出以下输出:
[1] "Output from R with format and paste: 1"
注意,字符串的前导零被切断了。
另一方面,这些行将按预期工作:
print("Output from python with format: {}".format(string_to_pass))
robjects.r('''print("Output from R with format: {}")'''.format(string_to_pass))
robjects.r('''print(paste("Output from R with pasting a string:", "01"))''')
输出:
Output from python with format: 01
[1] "Output from R with format: 01"
[1] "Output from R with pasting a string: 01"
有人可以解释一下这里发生了什么,只有在结合 python .format() 和 r paste() 时,以及如何避免它?
【问题讨论】:
标签: python r python-3.x string rpy2