Home > Back-end >  How to write 2D list to a txt file?
How to write 2D list to a txt file?

Time:10-29

I want to write 2D below to a txt file called readme.txt, but I keep on receiving error.

report_storage = [
    ['      ', '| G02  ', '| G05  ', '| G07  ', '| G08  ', '| G10  ', '| G25  '],
    ['------', '|------', '|------', '|------', '|------', '|------', '|------'],
    ['P001  ', '|  1   ', '|  0   ', '|      ', '|  --  ', '|  1   ', '|  0   '],
    ['P067  ', '|  1   ', '|  1   ', '|  0   ', '|      ', '|  --  ', '|  1   '],
    ['P218  ', '|  0   ', '|  1   ', '|  1   ', '|      ', '|      ', '|  1   '],
    ['P101  ', '|  0   ', '|  0   ', '|  1   ', '|  1   ', '|  --  ', '|  1   '],
    ['P456  ', '|  1   ', '|  1   ', '|      ', '|  1   ', '|      ', '|  0   '],
    ['------', '|------', '|------', '|------', '|------', '|------', '|------']
]

My code:

with open('readme.txt', 'w') as f:
    f.write(report_storage)

My code Output:

Traceback (most recent call last):
  File "D:\projects\FinalCodingChallenge\test4.0.py", line 70, in <module>
    f.write(report_storage)
TypeError: write() argument must be str, not list

My aim is to get the result below to appear on readme.txt

      | G02  | G05  | G07  | G08  | G10  | G25  
------|------|------|------|------|------|------
P001  |  1   |  0   |      |  --  |  1   |  0   
P067  |  1   |  1   |  0   |      |  --  |  1   
P218  |  0   |  1   |  1   |      |      |  1   
P101  |  0   |  0   |  1   |  1   |  --  |  1   
P456  |  1   |  1   |      |  1   |      |  0   
------|------|------|------|------|------|------

CodePudding user response:

report_storage is a list (of lists).

write() only accepts a string as parameter, not a list. Hence the TypeError.

You need to convert the list into string. First, convert each item in the list into string by joining cells with empty string between each cell.

report_storage_lines = [''.join(line) for line in report_storage]

Then join the lines with a linebreak character between each line:

report_storage_text = '\n'.join(report_storage_lines)

Then you can write to file:

with open('readme.txt', 'w') as f:
    f.write(report_storage_text)

CodePudding user response:

You can only write strings to a file. Your report_storage is a list of strings.

You need a loop to iterate over your list:

f = open('readme.txt', 'w')
for i in report_storage:
    tmp = ""
    for j in i:
        tmp  = j
    f.write(tmp   '\n')

CodePudding user response:

The f.write() syntax, as mentioned above, only accepts a string as an argument. Suppose you have a list of lines to write, then you would need to do '\n'.join(lines) so that you have a multi-line string representing the file contents to save.

That said, either of the below approaches should work to get you the desired output to a file.

The first approach uses a list comprehension to convert the list of lists to a string:

f.write('\n'.join([''.join(sub_list) for sub_list in report_storage]))

I personally prefer this approach, which uses the map builtin for slightly shorter syntax:

f.write('\n'.join(map(''.join, report_storage)))
  • Related