Reputation: 467
I would like to extract the first 6 characters in the filenames and add those as a column in a csv file. For example, filename: "CFL200_ABCD (2018-01-01).csv" with content:
Date, Time, Age 1/1/2001, 10:00 AM, 30 1/5/2006, 5:00 PM, 25
I want to create a new file with content:
ID, Date, Time, Count CFL200, 1/1/2001, 10:00 AM, 30 CFL200, 1/5/2006, 5:00 PM, 25
Could anyone please show me how to do this in Python? Thank you.
Upvotes: 0
Views: 845
Reputation: 1841
You can try using the pandas
package
import pandas
filename = 'CFL200_ABCD (2018-01-01).csv'
df = pandas.read_csv(filename)
df.insert(0, 'ID', filename[:6])
df.to_csv(filename, index=False)
Upvotes: 1