Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Below is your python code :
- import pandas as pd
- def readmyfile():
- # reading csv file
- result = pd.read_csv('data.csv')
- dates = [] # list to store elements in first column
- colors = [] # list to store elements in last column
- # iterating over first and last column
- for i in range(result.shape[0]):
- dates.append(result.iloc[i,0])
- colors.append(result.iloc[i,-1])
- # returning both lists
- return dates,colors
- def writetofile(output_file,dates,colors):
- # writing first row title
- output_file.write("Dates\tColors\n")
- # writing the two lists to the txt file
- for i in range(len(dates)):
- line = str(dates[i])+"\t"+str(colors[i])+"\n"
- output_file.write(line)
- if __name__ == '__main__':
- dates,colors = readmyfile()
- # opening the txt file
- file1 = open("Dates_Colors.txt","w")
- writetofile(file1,dates,colors)
- file1.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement