Borislav Hadzhiev
Wed Apr 20 2022·2 min read
Photo by Oleksii S
The Python "TypeError: write() argument must be str, not tuple" occurs when we
try to write a tuple
object to a file using the write()
method. To solve the
error, use the join()
method to join the tuple into a string, e.g.
my_file.write(','.join(my_tuple))
.
Here is an example of how the error occurs.
with open('example.txt', 'w', encoding='utf-8') as my_file: my_tuple = ('Alice', 'Bob', 'Carl') # ⛔️ TypeError: write() argument must be str, not dict my_file.write(my_tuple)
write()
method, but the method can only be called with a string argument.One way to solve the error is to use the str.join()
method to join the tuple
into a string.
with open('example.txt', 'w', encoding='utf-8') as my_file: my_tuple = ('Alice', 'Bob', 'Carl') my_file.write(','.join(my_tuple)) # 👉️ Alice,Bob,Carl
We used a comma as the separator between the items of the tuple, but you can use any other separator or an empty string if you don't need a separator between the elements.
The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
TypeError
if there are any non-string values in the iterable.If your tuple contains numbers, or other types, convert all of the values to
string before calling join()
.
my_tuple = ('a', 'b', 1, 2) all_strings = tuple(map(str, my_tuple)) print(all_strings) # 👉️ ('a', 'b', '1', '2') result = ''.join(all_strings) print(result) # 👉️ 'ab12'
The string the method is called on is used as the separator between elements.
my_tuple = ('a', 'b', 'c') result = '-'.join(my_tuple) print(result) # 👉️ 'a-b-c'
If you don't need a separator and just want to join the iterable's elements into
a string, call the join()
method on an empty string.
my_tuple = ('a', 'b', 'c') result = ''.join(my_tuple) print(result) # 👉️ 'abc'
Alternatively, you can pass the tuple to the str()
class to convert it to a
string before writing it to the file.
with open('example.txt', 'w', encoding='utf-8') as my_file: my_tuple = ('Alice', 'Bob', 'Carl') my_file.write(str(my_tuple)) # 👉️ ('Alice', 'Bob', 'Carl')
You can also convert the tuple to a JSON string before writing it to the file.
import json with open('example.txt', 'w', encoding='utf-8') as my_file: my_tuple = ('Alice', 'Bob', 'Carl') my_file.write(json.dumps(my_tuple)) # 👉️ ["Alice", "Bob", "Carl"]
The json.dumps method converts a Python object to a JSON formatted string.
Since JSON doesn't support tuples, the tuple gets converted to a list.
In case you declared a tuple by mistake, tuples are constructed in multiple ways:
()
creates an empty tuplea,
or (a,)
a, b
or (a, b)
tuple()
constructorIf you aren't sure what type of object a variable stores, use the type()
class.
my_tuple = 'hello', 'world' print(type(my_tuple)) # 👉️ <class 'tuple'> print(isinstance(my_tuple, tuple)) # 👉️ True my_str = 'hello world' print(type(my_str)) # 👉️ <class 'str'> print(isinstance(my_str, str)) # 👉️ True
The type class returns the type of an object.
The isinstance
function returns True
if the passed in object is an instance or a subclass of
the passed in class.