简体   繁体   中英

Python: Read CSV file select columns and create multiple text file

I have a CSV file with 2 columns ID and Info . I want to create multiple text files. Get the ID column as the filename and the Info column as the file content.

My csv file:

ID          Info
an_0000     No-19032745 2.20s 
an_0001     No-42037578 2.54s 
an_0002     No-56034123 6.71s
an_0003     No-87036247 1.91s
an_0004     No-13032757 1.15s
an_0005     No-44032657 1.91s
an_0006     No-14038488 5.11s
an_0007     No-19039678 1.20s
an_0008     No-53031654 2.50s

The filename i want:

an_0000.txt
... 

The file content i want:

No-19032745 2.20s 
...

This should work.

import pandas as pd
df= pd.read_csv("filename.csv")
for filename, content in zip(df['ID'],df['Info']):
    with open(filename+".txt",'w') as fp:
        fp.write(content)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM