Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;Don';“我不知道如何转换参数”;Python中的错误_Python_Python 3.x_Itertools - Fatal编程技术网

&引用;Don';“我不知道如何转换参数”;Python中的错误

&引用;Don';“我不知道如何转换参数”;Python中的错误,python,python-3.x,itertools,Python,Python 3.x,Itertools,当我运行下面的代码时,我得到“不知道如何转换参数”错误。我想把我的名字删掉。请帮忙 import ctypes from itertools import islice i = "sakthi srinivasan" sliced_text = islice(i, 0, 6) ctypes.windll.user32.MessageBoxW(0, sliced_text, "MsgBox", 0) MessageBoxW的第二个参数是string1。函数itertools.islice返回

当我运行下面的代码时,我得到“不知道如何转换参数”错误。我想把我的名字删掉。请帮忙

import ctypes
from itertools import islice

i = "sakthi srinivasan"
sliced_text =  islice(i, 0, 6)
ctypes.windll.user32.MessageBoxW(0, sliced_text, "MsgBox", 0)

MessageBoxW
的第二个参数是string1。函数
itertools.islice
返回对
i
中字符的迭代器。由于ctypes无法确定如何将其转换为字符数组,因此会抛出错误“不知道如何转换参数…”

您可以将迭代器转换为带有
'.join(切片文本)
的字符串,但最好的方法是通过将
i[0:6]
作为第二个参数来使用Python字符串中内置的切片。这是Python中获取子字符串的标准方法。

1从技术上讲,LPCTSTR是一个
const char*
const wchar\u t*
,这取决于您是否以UNICODE配置编译。

消息框希望您为其提供一个字符串,而不是Python迭代器对象。只要把它传给我[0:6],它就行了!谢谢:)Simon Hibbs在评论中回答了OP的问题,但我想我会为未来的读者提供一个更详细的答案。这也将避免这看起来像一个未回答的问题。