Writing list of lists to a text file, one list at a time
up vote
0
down vote
favorite
I have the code below:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')
How can I get to extract one list from 'data', which is a list of lists, write it in one line and move to next line before extracting the other list? I need the text file to look like:
24 Sale 0 15
16 Buy 18 0
python list tuples
add a comment |
up vote
0
down vote
favorite
I have the code below:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')
How can I get to extract one list from 'data', which is a list of lists, write it in one line and move to next line before extracting the other list? I need the text file to look like:
24 Sale 0 15
16 Buy 18 0
python list tuples
2
What's wrong with your current code?
– scnerd
Nov 7 at 13:31
1
missing a linefeed, basically
– Jean-François Fabre
Nov 7 at 13:49
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have the code below:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')
How can I get to extract one list from 'data', which is a list of lists, write it in one line and move to next line before extracting the other list? I need the text file to look like:
24 Sale 0 15
16 Buy 18 0
python list tuples
I have the code below:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')
How can I get to extract one list from 'data', which is a list of lists, write it in one line and move to next line before extracting the other list? I need the text file to look like:
24 Sale 0 15
16 Buy 18 0
python list tuples
python list tuples
asked Nov 7 at 13:30
pro1991
114
114
2
What's wrong with your current code?
– scnerd
Nov 7 at 13:31
1
missing a linefeed, basically
– Jean-François Fabre
Nov 7 at 13:49
add a comment |
2
What's wrong with your current code?
– scnerd
Nov 7 at 13:31
1
missing a linefeed, basically
– Jean-François Fabre
Nov 7 at 13:49
2
2
What's wrong with your current code?
– scnerd
Nov 7 at 13:31
What's wrong with your current code?
– scnerd
Nov 7 at 13:31
1
1
missing a linefeed, basically
– Jean-François Fabre
Nov 7 at 13:49
missing a linefeed, basically
– Jean-François Fabre
Nov 7 at 13:49
add a comment |
5 Answers
5
active
oldest
votes
up vote
3
down vote
accepted
I think you could just write a newline just after exiting the inner loop, and your code would work (even if the last item would have spaces after it).
for item in sub_list: #Since attempt to write whole giv error
x.write(str(item) + ' ')
x.write('n')
However, built-in csv
module handles list of strings, integers, whatever, automatically (also handles strings with spaces in it by quoting them):
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w', newline="") as x:
csv.writer(x,delimiter=" ").writerows(data)
the writerows
method is able to write a collection of tuples or lists. Exactly what you need. The only difference is that csv
cannot separate the data by more than one char (multi-char separators aren't allowed). You'll have to make do with 1 space.
Very helpful exiting the loop does the trick.
– pro1991
Nov 11 at 17:45
you can accept the answer if it solved your problem.
– Jean-François Fabre
Nov 11 at 18:51
add a comment |
up vote
2
down vote
The most concise way to do this would be the following:
with open('txt_file.txt', 'w') as x:
x.write('n'.join(' '.join(map(str, row)) for row in data))
well, I don't think it works with OP data.
– Jean-François Fabre
Nov 7 at 13:37
Oh, forgot to format things...
– scnerd
Nov 7 at 13:44
yeah, that should work
– Jean-François Fabre
Nov 7 at 13:47
But it's not elegant, and your csv solution handles the nitty gritty crap better
– scnerd
Nov 7 at 13:57
only a raw solution is able to create a multi-spaced separator, that said.
– Jean-François Fabre
Nov 7 at 13:57
add a comment |
up vote
0
down vote
try this:
with open('txt_file.txt','wt') as f:
[f.write(' '.join([str(it) for it in item])+'n') for item in data]
don't use comprehension just for looping. besides it doesn't work with OP data.
– Jean-François Fabre
Nov 7 at 13:38
fixed for OP's case
– vencaslac
Nov 7 at 14:14
please test your solution, because it's not working properly either.
– Jean-François Fabre
Nov 7 at 14:15
fixed again, but i agree it's becoming more and more convoluted
– vencaslac
Nov 7 at 14:31
at least it works. Considerf.writelines
instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect
– Jean-François Fabre
Nov 7 at 14:41
add a comment |
up vote
-1
down vote
Mine approach is based on your idea with a bit of adjustments (1. use format template so you can adjust final view and 2) using unpacking.) Final code looks like, apart make proper format template:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
FMT = '{} {} {}'
with open('txt_file.txt', 'w') as x:
for item in data:
record = FMT.format(*item)
print(record)
x.write(record + 'n')
FMT = '{} {} {}'.format({}, {}, {})
??? why not justFMT = "{} {} {}"
? plus have you tested your code?
– Jean-François Fabre
Nov 7 at 13:48
good point. not fully.
– Сергей Николаевич
Nov 7 at 13:53
ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
– Jean-François Fabre
Nov 7 at 13:56
that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
– Сергей Николаевич
Nov 7 at 14:01
add a comment |
up vote
-2
down vote
"n"
does the trick:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')
x.write("n")
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
I think you could just write a newline just after exiting the inner loop, and your code would work (even if the last item would have spaces after it).
for item in sub_list: #Since attempt to write whole giv error
x.write(str(item) + ' ')
x.write('n')
However, built-in csv
module handles list of strings, integers, whatever, automatically (also handles strings with spaces in it by quoting them):
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w', newline="") as x:
csv.writer(x,delimiter=" ").writerows(data)
the writerows
method is able to write a collection of tuples or lists. Exactly what you need. The only difference is that csv
cannot separate the data by more than one char (multi-char separators aren't allowed). You'll have to make do with 1 space.
Very helpful exiting the loop does the trick.
– pro1991
Nov 11 at 17:45
you can accept the answer if it solved your problem.
– Jean-François Fabre
Nov 11 at 18:51
add a comment |
up vote
3
down vote
accepted
I think you could just write a newline just after exiting the inner loop, and your code would work (even if the last item would have spaces after it).
for item in sub_list: #Since attempt to write whole giv error
x.write(str(item) + ' ')
x.write('n')
However, built-in csv
module handles list of strings, integers, whatever, automatically (also handles strings with spaces in it by quoting them):
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w', newline="") as x:
csv.writer(x,delimiter=" ").writerows(data)
the writerows
method is able to write a collection of tuples or lists. Exactly what you need. The only difference is that csv
cannot separate the data by more than one char (multi-char separators aren't allowed). You'll have to make do with 1 space.
Very helpful exiting the loop does the trick.
– pro1991
Nov 11 at 17:45
you can accept the answer if it solved your problem.
– Jean-François Fabre
Nov 11 at 18:51
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
I think you could just write a newline just after exiting the inner loop, and your code would work (even if the last item would have spaces after it).
for item in sub_list: #Since attempt to write whole giv error
x.write(str(item) + ' ')
x.write('n')
However, built-in csv
module handles list of strings, integers, whatever, automatically (also handles strings with spaces in it by quoting them):
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w', newline="") as x:
csv.writer(x,delimiter=" ").writerows(data)
the writerows
method is able to write a collection of tuples or lists. Exactly what you need. The only difference is that csv
cannot separate the data by more than one char (multi-char separators aren't allowed). You'll have to make do with 1 space.
I think you could just write a newline just after exiting the inner loop, and your code would work (even if the last item would have spaces after it).
for item in sub_list: #Since attempt to write whole giv error
x.write(str(item) + ' ')
x.write('n')
However, built-in csv
module handles list of strings, integers, whatever, automatically (also handles strings with spaces in it by quoting them):
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w', newline="") as x:
csv.writer(x,delimiter=" ").writerows(data)
the writerows
method is able to write a collection of tuples or lists. Exactly what you need. The only difference is that csv
cannot separate the data by more than one char (multi-char separators aren't allowed). You'll have to make do with 1 space.
edited Nov 7 at 13:47
RoadRunner
8,94831138
8,94831138
answered Nov 7 at 13:35
Jean-François Fabre
97.7k950107
97.7k950107
Very helpful exiting the loop does the trick.
– pro1991
Nov 11 at 17:45
you can accept the answer if it solved your problem.
– Jean-François Fabre
Nov 11 at 18:51
add a comment |
Very helpful exiting the loop does the trick.
– pro1991
Nov 11 at 17:45
you can accept the answer if it solved your problem.
– Jean-François Fabre
Nov 11 at 18:51
Very helpful exiting the loop does the trick.
– pro1991
Nov 11 at 17:45
Very helpful exiting the loop does the trick.
– pro1991
Nov 11 at 17:45
you can accept the answer if it solved your problem.
– Jean-François Fabre
Nov 11 at 18:51
you can accept the answer if it solved your problem.
– Jean-François Fabre
Nov 11 at 18:51
add a comment |
up vote
2
down vote
The most concise way to do this would be the following:
with open('txt_file.txt', 'w') as x:
x.write('n'.join(' '.join(map(str, row)) for row in data))
well, I don't think it works with OP data.
– Jean-François Fabre
Nov 7 at 13:37
Oh, forgot to format things...
– scnerd
Nov 7 at 13:44
yeah, that should work
– Jean-François Fabre
Nov 7 at 13:47
But it's not elegant, and your csv solution handles the nitty gritty crap better
– scnerd
Nov 7 at 13:57
only a raw solution is able to create a multi-spaced separator, that said.
– Jean-François Fabre
Nov 7 at 13:57
add a comment |
up vote
2
down vote
The most concise way to do this would be the following:
with open('txt_file.txt', 'w') as x:
x.write('n'.join(' '.join(map(str, row)) for row in data))
well, I don't think it works with OP data.
– Jean-François Fabre
Nov 7 at 13:37
Oh, forgot to format things...
– scnerd
Nov 7 at 13:44
yeah, that should work
– Jean-François Fabre
Nov 7 at 13:47
But it's not elegant, and your csv solution handles the nitty gritty crap better
– scnerd
Nov 7 at 13:57
only a raw solution is able to create a multi-spaced separator, that said.
– Jean-François Fabre
Nov 7 at 13:57
add a comment |
up vote
2
down vote
up vote
2
down vote
The most concise way to do this would be the following:
with open('txt_file.txt', 'w') as x:
x.write('n'.join(' '.join(map(str, row)) for row in data))
The most concise way to do this would be the following:
with open('txt_file.txt', 'w') as x:
x.write('n'.join(' '.join(map(str, row)) for row in data))
edited Nov 7 at 13:44
answered Nov 7 at 13:32
scnerd
3,0011820
3,0011820
well, I don't think it works with OP data.
– Jean-François Fabre
Nov 7 at 13:37
Oh, forgot to format things...
– scnerd
Nov 7 at 13:44
yeah, that should work
– Jean-François Fabre
Nov 7 at 13:47
But it's not elegant, and your csv solution handles the nitty gritty crap better
– scnerd
Nov 7 at 13:57
only a raw solution is able to create a multi-spaced separator, that said.
– Jean-François Fabre
Nov 7 at 13:57
add a comment |
well, I don't think it works with OP data.
– Jean-François Fabre
Nov 7 at 13:37
Oh, forgot to format things...
– scnerd
Nov 7 at 13:44
yeah, that should work
– Jean-François Fabre
Nov 7 at 13:47
But it's not elegant, and your csv solution handles the nitty gritty crap better
– scnerd
Nov 7 at 13:57
only a raw solution is able to create a multi-spaced separator, that said.
– Jean-François Fabre
Nov 7 at 13:57
well, I don't think it works with OP data.
– Jean-François Fabre
Nov 7 at 13:37
well, I don't think it works with OP data.
– Jean-François Fabre
Nov 7 at 13:37
Oh, forgot to format things...
– scnerd
Nov 7 at 13:44
Oh, forgot to format things...
– scnerd
Nov 7 at 13:44
yeah, that should work
– Jean-François Fabre
Nov 7 at 13:47
yeah, that should work
– Jean-François Fabre
Nov 7 at 13:47
But it's not elegant, and your csv solution handles the nitty gritty crap better
– scnerd
Nov 7 at 13:57
But it's not elegant, and your csv solution handles the nitty gritty crap better
– scnerd
Nov 7 at 13:57
only a raw solution is able to create a multi-spaced separator, that said.
– Jean-François Fabre
Nov 7 at 13:57
only a raw solution is able to create a multi-spaced separator, that said.
– Jean-François Fabre
Nov 7 at 13:57
add a comment |
up vote
0
down vote
try this:
with open('txt_file.txt','wt') as f:
[f.write(' '.join([str(it) for it in item])+'n') for item in data]
don't use comprehension just for looping. besides it doesn't work with OP data.
– Jean-François Fabre
Nov 7 at 13:38
fixed for OP's case
– vencaslac
Nov 7 at 14:14
please test your solution, because it's not working properly either.
– Jean-François Fabre
Nov 7 at 14:15
fixed again, but i agree it's becoming more and more convoluted
– vencaslac
Nov 7 at 14:31
at least it works. Considerf.writelines
instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect
– Jean-François Fabre
Nov 7 at 14:41
add a comment |
up vote
0
down vote
try this:
with open('txt_file.txt','wt') as f:
[f.write(' '.join([str(it) for it in item])+'n') for item in data]
don't use comprehension just for looping. besides it doesn't work with OP data.
– Jean-François Fabre
Nov 7 at 13:38
fixed for OP's case
– vencaslac
Nov 7 at 14:14
please test your solution, because it's not working properly either.
– Jean-François Fabre
Nov 7 at 14:15
fixed again, but i agree it's becoming more and more convoluted
– vencaslac
Nov 7 at 14:31
at least it works. Considerf.writelines
instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect
– Jean-François Fabre
Nov 7 at 14:41
add a comment |
up vote
0
down vote
up vote
0
down vote
try this:
with open('txt_file.txt','wt') as f:
[f.write(' '.join([str(it) for it in item])+'n') for item in data]
try this:
with open('txt_file.txt','wt') as f:
[f.write(' '.join([str(it) for it in item])+'n') for item in data]
edited Nov 7 at 14:24
answered Nov 7 at 13:34
vencaslac
1,008217
1,008217
don't use comprehension just for looping. besides it doesn't work with OP data.
– Jean-François Fabre
Nov 7 at 13:38
fixed for OP's case
– vencaslac
Nov 7 at 14:14
please test your solution, because it's not working properly either.
– Jean-François Fabre
Nov 7 at 14:15
fixed again, but i agree it's becoming more and more convoluted
– vencaslac
Nov 7 at 14:31
at least it works. Considerf.writelines
instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect
– Jean-François Fabre
Nov 7 at 14:41
add a comment |
don't use comprehension just for looping. besides it doesn't work with OP data.
– Jean-François Fabre
Nov 7 at 13:38
fixed for OP's case
– vencaslac
Nov 7 at 14:14
please test your solution, because it's not working properly either.
– Jean-François Fabre
Nov 7 at 14:15
fixed again, but i agree it's becoming more and more convoluted
– vencaslac
Nov 7 at 14:31
at least it works. Considerf.writelines
instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect
– Jean-François Fabre
Nov 7 at 14:41
don't use comprehension just for looping. besides it doesn't work with OP data.
– Jean-François Fabre
Nov 7 at 13:38
don't use comprehension just for looping. besides it doesn't work with OP data.
– Jean-François Fabre
Nov 7 at 13:38
fixed for OP's case
– vencaslac
Nov 7 at 14:14
fixed for OP's case
– vencaslac
Nov 7 at 14:14
please test your solution, because it's not working properly either.
– Jean-François Fabre
Nov 7 at 14:15
please test your solution, because it's not working properly either.
– Jean-François Fabre
Nov 7 at 14:15
fixed again, but i agree it's becoming more and more convoluted
– vencaslac
Nov 7 at 14:31
fixed again, but i agree it's becoming more and more convoluted
– vencaslac
Nov 7 at 14:31
at least it works. Consider
f.writelines
instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect– Jean-François Fabre
Nov 7 at 14:41
at least it works. Consider
f.writelines
instead of a comprehension. And pass the generator expression to writelines. Much better than using a comprehsnion just for side effect– Jean-François Fabre
Nov 7 at 14:41
add a comment |
up vote
-1
down vote
Mine approach is based on your idea with a bit of adjustments (1. use format template so you can adjust final view and 2) using unpacking.) Final code looks like, apart make proper format template:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
FMT = '{} {} {}'
with open('txt_file.txt', 'w') as x:
for item in data:
record = FMT.format(*item)
print(record)
x.write(record + 'n')
FMT = '{} {} {}'.format({}, {}, {})
??? why not justFMT = "{} {} {}"
? plus have you tested your code?
– Jean-François Fabre
Nov 7 at 13:48
good point. not fully.
– Сергей Николаевич
Nov 7 at 13:53
ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
– Jean-François Fabre
Nov 7 at 13:56
that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
– Сергей Николаевич
Nov 7 at 14:01
add a comment |
up vote
-1
down vote
Mine approach is based on your idea with a bit of adjustments (1. use format template so you can adjust final view and 2) using unpacking.) Final code looks like, apart make proper format template:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
FMT = '{} {} {}'
with open('txt_file.txt', 'w') as x:
for item in data:
record = FMT.format(*item)
print(record)
x.write(record + 'n')
FMT = '{} {} {}'.format({}, {}, {})
??? why not justFMT = "{} {} {}"
? plus have you tested your code?
– Jean-François Fabre
Nov 7 at 13:48
good point. not fully.
– Сергей Николаевич
Nov 7 at 13:53
ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
– Jean-François Fabre
Nov 7 at 13:56
that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
– Сергей Николаевич
Nov 7 at 14:01
add a comment |
up vote
-1
down vote
up vote
-1
down vote
Mine approach is based on your idea with a bit of adjustments (1. use format template so you can adjust final view and 2) using unpacking.) Final code looks like, apart make proper format template:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
FMT = '{} {} {}'
with open('txt_file.txt', 'w') as x:
for item in data:
record = FMT.format(*item)
print(record)
x.write(record + 'n')
Mine approach is based on your idea with a bit of adjustments (1. use format template so you can adjust final view and 2) using unpacking.) Final code looks like, apart make proper format template:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
FMT = '{} {} {}'
with open('txt_file.txt', 'w') as x:
for item in data:
record = FMT.format(*item)
print(record)
x.write(record + 'n')
edited Nov 7 at 13:54
answered Nov 7 at 13:46
Сергей Николаевич
1046
1046
FMT = '{} {} {}'.format({}, {}, {})
??? why not justFMT = "{} {} {}"
? plus have you tested your code?
– Jean-François Fabre
Nov 7 at 13:48
good point. not fully.
– Сергей Николаевич
Nov 7 at 13:53
ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
– Jean-François Fabre
Nov 7 at 13:56
that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
– Сергей Николаевич
Nov 7 at 14:01
add a comment |
FMT = '{} {} {}'.format({}, {}, {})
??? why not justFMT = "{} {} {}"
? plus have you tested your code?
– Jean-François Fabre
Nov 7 at 13:48
good point. not fully.
– Сергей Николаевич
Nov 7 at 13:53
ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
– Jean-François Fabre
Nov 7 at 13:56
that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
– Сергей Николаевич
Nov 7 at 14:01
FMT = '{} {} {}'.format({}, {}, {})
??? why not just FMT = "{} {} {}"
? plus have you tested your code?– Jean-François Fabre
Nov 7 at 13:48
FMT = '{} {} {}'.format({}, {}, {})
??? why not just FMT = "{} {} {}"
? plus have you tested your code?– Jean-François Fabre
Nov 7 at 13:48
good point. not fully.
– Сергей Николаевич
Nov 7 at 13:53
good point. not fully.
– Сергей Николаевич
Nov 7 at 13:53
ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
– Jean-François Fabre
Nov 7 at 13:56
ahem. There are 4 arguments, not 3. why not including the linefeed in your format?
– Jean-François Fabre
Nov 7 at 13:56
that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
– Сергей Николаевич
Nov 7 at 14:01
that would write properly but not print if not specify end. After all idea still the same buy using format template and unpacking.
– Сергей Николаевич
Nov 7 at 14:01
add a comment |
up vote
-2
down vote
"n"
does the trick:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')
x.write("n")
add a comment |
up vote
-2
down vote
"n"
does the trick:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')
x.write("n")
add a comment |
up vote
-2
down vote
up vote
-2
down vote
"n"
does the trick:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')
x.write("n")
"n"
does the trick:
data=[(24, 'Sale', 0, 15), (16, 'Buy', 18, 0)]
with open('txt_file.txt', 'w') as x:
for sub_list in data:
for item in sub_list: #Since attempt to write whole giv error
print(item)
x.write(str(item) + ' ')
x.write("n")
edited Nov 7 at 14:02
answered Nov 7 at 13:43
Suhas NM
875
875
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190443%2fwriting-list-of-lists-to-a-text-file-one-list-at-a-time%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
What's wrong with your current code?
– scnerd
Nov 7 at 13:31
1
missing a linefeed, basically
– Jean-François Fabre
Nov 7 at 13:49