C 到 python 代码转换

标签 c python-3.x

我正在进行从 C 到 python 的代码转换... 我有一个 char 数组,用作字符串缓冲区

 char str[25]; int i=0;

 str[i]='\0';

这里我将采用不同的值,甚至 str[i] 也持有不同的值

我想要Python中的等效代码...就像一个字符串缓冲区,我可以在其中存储n个编辑字符串内容。我什至尝试使用列表,但它不是很有效,所以还有其他出路吗? Python中有字符串缓冲区吗?如果是的话我该如何根据这些使用它?

最佳答案

在 Python 中使用 bytearray 存储可变的字节数据列表:

s = bytearray(b'My string')
print(s)
s[3] = ord('f')  # bytes are data not characters, so get byte value
print(s)
print(s.decode('ascii')) # To display as string

输出:

bytearray(b'My string')
bytearray(b'My ftring')
My ftring

如果您需要改变 Unicode 字符串数据,那么 list 就是正确的选择:

s = list('My string')
s[3] = 'f'
print(''.join(s))

输出:

My ftring

关于C 到 python 代码转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19638305/

相关文章:

c - 简单的 C 程序用户输入 5 个 friend 的名字

关于 char 数组的说明

python - PyCharm 导入方式与系统命令提示符 (Windows) 有何不同

python - 防止将脚本目录添加到 Python 3 中的 sys.path

c++ - 如何计算文本文件中的所有数字?

c - 为什么 llvm 和 gcc 在 x86 64 上使用不同的函数序言?

c++ - 第二次检查后 flock lock 丢失

python - 如何让 random.choice 在 Python 中打印出相同的结果?

python - Python错误处理重试代码

Python 子进程将输出返回为 stderr