Python Forum
reading csv and store it into different variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading csv and store it into different variable
#1
#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
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with threading and reading variable from a different script Lembas 14 3,047 Apr-26-2023, 11:21 PM
Last Post: Lembas
  store all variable values into list and insert to sql_summary table mg24 3 1,147 Sep-28-2022, 09:13 AM
Last Post: Larz60+
  Store variable data and display sum after 60 seconds the_dude 11 3,460 Dec-16-2021, 07:07 PM
Last Post: deanhystad
  Python reading variable in another py file wrongly _vertig0 2 1,971 Nov-21-2020, 07:19 AM
Last Post: _vertig0
  Reading blob data from database by python and store it in .zip format Adityasi 2 6,614 Nov-18-2019, 05:22 PM
Last Post: ibreeden
  How to store the value from variable into list & run a statement then put in variable searching1 1 2,456 May-29-2019, 06:36 AM
Last Post: heiner55
  search and store a value from linux command output in to variable python prazy29 1 3,278 Apr-06-2018, 01:56 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020