Write data in one line

前端 未结 1 758
猫巷女王i
猫巷女王i 2021-01-29 12:30

I have data written in a csv file in the format below:

[(789,255,25,33.0),(855,275,25,33.0)............]

I want it to be converted into a forma

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-29 12:54

    Using the csv module and enumerate.

    Ex:

    import csv
    data = [(789,255,25,33.0),(855,275,25,33.0)]
    with open(filename, "w") as outfile:
        writer = csv.writer(outfile)
        for i, line in enumerate(data, 1):
            writer.writerow([i]+ list(line))
    

    Using Pandas

    import pandas as pd
    data = [(789,255,25,33.0),(855,275,25,33.0)]
    df = pd.DataFrame(data)
    df.to_csv(filename, header=None)
    

    0 讨论(0)
提交回复
热议问题