Transposing a text file in Python

后端 未结 3 1285
梦谈多话
梦谈多话 2021-01-21 12:17

I have a text file that I would like to transpose using Python.

For example, given the following file:

asdfg
qwert

I would like the out

3条回答
  •  清歌不尽
    2021-01-21 13:19

    Maybe this is you wanted:

    with open("/Users/wy/Desktop/wy.txt", "r") as cin:
        lines = cin.read()
    lineStr = lines.split('\n')
    
    with open("/Users/wy/Desktop/res.txt", "w") as cout:
        for ele in zip(*lineStr):
            cout.write(''.join(list(ele)) + '\n')
    

提交回复
热议问题