Convert a space delimited file to comma separated values file in python

前端 未结 8 1049
陌清茗
陌清茗 2021-01-04 06:11

I am very new to Python. I know that this has already been asked, and I apologise, but the difference in this new situation is that spaces between strings are not equal. I

8条回答
  •  不知归路
    2021-01-04 06:33

    replace your first bit with this. it's not super pretty but it will give you a csv format.

    with open('coord') as infile, open('coordv', 'w') as outfile:
        for line in infile:
            outfile.write(" ".join(line.split()).replace(' ', ','))
            outfile.write(",") # trailing comma shouldn't matter
    

    if you want the outfile to have everything on different lines you could add outfile.write("\n") at the end of the for loop, but i dont think your code that follows this will work with it like that.

提交回复
热议问题