how to create filename by using a variable ?

This is the place for queries that don't fit in any of the other categories.

how to create filename by using a variable ?

Postby bigstyx » Fri Feb 22, 2013 9:54 pm

hello,
I am a newbie in Python ;
I would like to extract lines from a unique input file, to place them in several output files according to some specific string on each line;
it would look like the following pseudo-code (the 'line_list' variable being the list of strings in each line read from the input file)
if line_list[5] == 'B01' then write line into 'B01-output.txt' file
if line_list[5] == 'B02' then write line into 'B02-output.txt' file
if line_list[5] == 'B03' then write line into 'B03-output.txt' file
and so on until, let say, B99 ;

Question :
how could I write a Python script to simplify the psuedo-code above by using loops ?
this script, if it was possible, could look like this one :
Code: Select all
input_open = open('input_file.txt', 'r')
Bubble_List = ['B01','B02','B03','B04', ...,'B97','B98','B99']

for prefix in Bubble_List:
   prefix_output_file = prefix+'_output_file.txt'
   prefix_output_open = open(prefix_output_file,'w')  # how to make the prefix_output_open variable name varies with the value of prefix ?

for line in input_open:
   line_list = line.split()
   if line_list[5][:3] in Bubble_List:
      prefix_output_open.write(line)   # how to make the prefix_output_open variable name varies with the value of prefix ?


Hope my question is clear enough

thanks in advance
Last edited by Yoriz on Sat Feb 23, 2013 12:17 am, edited 1 time in total.
Reason: Changed the title
bigstyx
 
Posts: 2
Joined: Fri Feb 22, 2013 5:07 pm

Re: how to create varaible variable name ?

Postby Yoriz » Fri Feb 22, 2013 10:58 pm

This isn't tested as i don't have your files but maybe something like this.
Code: Select all
bubble_List = ['B01','B02','B03','B04', ...,'B97','B98','B99']

with open('input_file.txt', 'r') as read_file:
    for line in read_file:
        line_list = line.split()
        bubble = line_list[5][:3]
        if bubble in bubble_List:
            with open('{}-output_file.txt'.format(bubble), 'a') as write_file:
                write_file.write(line)


What you are looking for is string formatting, there is a tutorial on strings on the forum that has a part on formatting you'll find it here
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: how to create varaible variable name ?

Postby Somelauw » Fri Feb 22, 2013 11:18 pm

You are not really creating a variable variable name. You are creating a variable file name.
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 72
Joined: Tue Feb 12, 2013 8:30 pm

Re: how to create filename by using a variable ?

Postby bigstyx » Wed Mar 06, 2013 6:39 pm

I got the correct and simple example from another forum ;
here it is for anybody who would look for sucha thing :

Code: Select all
files = dict()
for x in range(10,99):
    name = 'R%02d-input-file.csv' % x
    print ('opening name: %s' % name) # to check filename
    files[x] = open(name,'w')
bigstyx
 
Posts: 2
Joined: Fri Feb 22, 2013 5:07 pm

Re: how to create filename by using a variable ?

Postby ichabod801 » Wed Mar 06, 2013 7:38 pm

Is opening 90 files at once really a good idea? I would read through the input file and instead of writing directly to the file save each files lines in a separate list. Then go through the list of file outputs, writing to each file once.

It would of course depend on the size of your input file, but I'd think you'd need a damn big input file before 90 files at once really became worthwhile.
Craig "Ichabod" O'Brien
Minimalist, buddhist, theist, and programmer
Current languages: Python, SAS, and C++
Previous serious languages: R, Java, VBA, Lisp, HyperTalk, BASIC
ichabod801
 
Posts: 84
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 3 guests