Python Forum

Full Version: reading csv and store it into different variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
#prompt user to input csv file title

x=input("please enter the title of your .csv file,key in 'QUIT' when done:")
i=0
while x != "QUIT":
    data_i=pd.read_csv("%s"%x)   
    i+=1
    x=input("please enter the title of your .csv file,key in 'QUIT' when done:")
my code are as above.

how to change my code so that when every csv file it read, it will store starting from data_0,data_1,data_2...
currently it keep on replace the csv file to the data_i, data_i will contain the last csv it read.

thanks
untested code
filenumber = 1
suffix = 'dat' # change to whatever (like out or txt)
while True:
    filename = input('Enter csv filename, or 'quit' when done: ')
    if filename == 'quit':
        break
    # read data here
    outfilename = 'data_{}.{}'format(filenumber, suffix)
    with open(outfilename, 'w') as ofp:
        ofp.write(data)
    filenumber += 1