简体   繁体   English

Python 3:将元组转换为字符串

[英]Python 3: Converting A Tuple To A String

I have the following code: 我有以下代码:

var_one = var_two[var_three-1]
var_one = "string_one" + var_1

And I need to do the following to it: 我需要做以下事情:

var_four = 'string_two', var_one

However, this returns the following error: 但是,这会返回以下错误:

TypeError: Can't convert 'tuple' object to str implicity

I have tried things such as str(var_one) and using strip but these did not work. 我已经尝试过诸如str(var_one)和使用strip但是这些都没有用。 What can I do to achieve the result I require? 我能做些什么才能达到我要求的效果?

EDIT - Here are what the variables contain: 编辑 - 以下是变量包含的内容:

var_one: new variable var_one:新变量

var_two: tuple var_two:元组

var_three: integer var_three:整数

var_four: new var_four:new

EDIT2: EDIT2:

The line in the program that makes the error is: os.system(var_four) 产生错误的程序中的行是: os.system(var_four)

one_big_string = ''.join(tuple)   
print one_big_string 

What you've written is fine: 你写的很好:

>>> x = 1
>>> y = 1, x
>>> 

The problem is that somewhere else in your code, you're using var_four as a string where it should be a tuple. 问题是你的代码中的其他地方,你使用var_four作为一个字符串,它应该是一个元组。

BTW, I think it's neater to put parentheses around tuples like this; 顺便说一下,我觉得把括号放在像这样的元组周围是很简洁的; otherwise I tend to think they're being used in tuple unpacking. 否则我倾向于认为它们被用于元组解包。


EDIT: There are all sorts of ways to join and format strings -- Python is good at that. 编辑:有各种各样的方法来加入和格式化字符串--Python很擅长。 In somewhat-decreasing order of generality: 在某种程度上降低的普遍性顺序:

"{first_thing} {second_thing}".format(first_thing=var_one, second_thing=var_two)

"{0} {1}".format(var_one, var_two)

var_one + var_two

Your code looks fine as is. 你的代码看起来很好。

Try running import pdb; pdb.set_trace() 尝试运行import pdb; pdb.set_trace() import pdb; pdb.set_trace() in your program to see if you can find the line triggering the error. import pdb; pdb.set_trace() ,看看是否可以找到触发错误的行。

EDIT: You'll want to use ''.join(var_four) to convert var_four into a string before adding it to whatever it is you want to use it. 编辑:您需要使用''.join(var_four)var_four转换为字符串,然后再将其添加到您想要使用它的任何内容中。 Please note that this will actually create a new string and not overwrite var_four . 请注意,这实际上会创建一个新字符串,而不是覆盖var_four See Python 3 string.join() equivalent? 请参阅Python 3 string.join()等效?

Also, you should be using the subprocess module instead of os.system . 此外,您应该使用subprocess os.system模块而不是os.system See the Python 3.x documentation . 请参阅Python 3.x文档

str(my_tuple)

This seems too easy, but this works in Python 3.6 这似乎太容易了,但这适用于Python 3.6

>>> x = list(range(100))
>>> y = list(range(500, 600))
>>> zip_obj = zip(x, y)
>>> my_tuple = tuple(zip_obj)
>>> type(my_tuple)
>>> <class 'tuple'>
>>> tuple_str = str(my_tuple)
>>> tuple_str
'((0, 500), (1, 501), (2, 502), (3, 503), (4, 504), (5, 505), (6, 506), (7, 507), (8, 508), (9, 509), (10, 510), (11, 511), (12, 512), (13, 513), (14, 514), (15, 515), (16, 516), (17, 517), (18, 518), (19, 519), (20, 520), (21, 521), (22, 522), (23, 523), (24, 524), (25, 525), (26, 526), (27, 527), (28, 528), (29, 529), (30, 530), (31, 531), (32, 532), (33, 533), (34, 534), (35, 535), (36, 536), (37, 537), (38, 538), (39, 539), (40, 540), (41, 541), (42, 542), (43, 543), (44, 544), (45, 545), (46, 546), (47, 547), (48, 548), (49, 549), (50, 550), (51, 551), (52, 552), (53, 553), (54, 554), (55, 555), (56, 556), (57, 557), (58, 558), (59, 559), (60, 560), (61, 561), (62, 562), (63, 563), (64, 564), (65, 565), (66, 566), (67, 567), (68, 568), (69, 569), (70, 570), (71, 571), (72, 572), (73, 573), (74, 574), (75, 575), (76, 576), (77, 577), (78, 578), (79, 579), (80, 580), (81, 581), (82, 582), (83, 583), (84, 584), (85, 585), (86, 586), (87, 587), (88, 588), (89, 589), (90, 590), (91, 591), (92, 592), (93, 593), (94, 594), (95, 595), (96, 596), (97, 597), (98, 598), (99, 599))'
>>> 

os.system expects a string which will will execute in the shell, but you're giving it a tuple instead. os.system需要一个将在shell中执行的字符串,但是你os.system它一个元组。

Imagine we want to run the command rm -rf /home/mike . 想象一下,我们想要运行命令rm -rf /home/mike You might be doing something like 你可能会做类似的事情

binary_and_option = 'rm -rf'
directory = '/home/mike'

command = binary_and_option, directory # This is the tuple 
                                       #    ('rm -rf', '/home/mike') 
                                       # it is NOT the string 
                                       #     'rm -rf /home/mike'

os.system(command) # this clearly won't work, since it's just 
                   # os.system(('rm -rf', '/home/mike')) 

what you want to do instead is 你想要做的是

command = "%d %d" % (binary_and_option, directory)

to assemble the string. 组装字符串。 You are probably thinking comma assembles str-ed objects together with spaces in between, but that's only for print ; 您可能认为逗号将stred ed对象与其间的空格组合在一起,但这仅用于print ; it's not how strings work in general. 一般来说,字符串不起作用。


But wait , there's more! 等等 ,还有更多! You never want to use os.system , especially when you're going to build commands. 永远不想使用os.system ,特别是当你要构建命令时。 It invokes the shell (which introduces unncessary security risks and other penalties) and has an inflexible API. 它调用shell(引入了不必要的安全风险和其他惩罚)并且具有不灵活的API。 Instead, use the subprocess module. 而是使用subprocess模块。

import subprocess 

binary_and_option = ['rm', '-rf']
directory = '/home/mike'

command = binary_and_option + [directory]

subprocess.call(command)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM