Get in touch!Get in touch!

Generate Sequential Data Using Python

Let us suppose that you have a string that you want to reproduce n times, with a prefix or suffix that increments (or decrements), for example:

  • String_001
  • String_002
  • String_003
  • String_004
  • String_… ,

then this script will help you accomplish just that. The output is a text file. You can modify the script accordingly if your incremental variable is in the middle of your text, for example:

  • foo_0001_bar
  • foo_0002_bar
  • foo_0003_bar
  • foo_0004_bar
  • foo_…._bar

Note:
You can also modify the script to save the generated data as a spreadsheet, see this post to see how this can be done.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python
 
#print_incremental_data.py
#Author: Victor Miti
#Tested with python 2.6.8 and 2.7.5
 
'''-----------------------------Description--------------------------------'''
#Let us suppose that you have a string that you want to reproduce n times,
#with a prefix or suffix that increments (or decrements), for example:
#   String_001
#   String_002
#   String_003
#   String_004
#   String_... ,
#then this script will help you accomplish just that. Output is a text file
#You can modify the script accordingly if your incremental variable is in the
#middle of your text, for example:
#   foo_0001_bar
#   foo_0002_bar
#   foo_0003_bar
#   foo_0004_bar
#   foo_...._bar
'''------------------------------------------------------------------------'''
 
import sys, datetime
 
#get the current time
now = datetime.datetime.now()
 
#File Name Prefix
file_prefix = "textReproduction_"
 
#redirect stdout to a .txt file in the same directory
#we want the filename to have a timestamp
sys.stdout = open(file_prefix+now.strftime("%Y%m%d~%H%M%S")+".txt", "w")
 
#The text you want to reproduce n times
text = "Foobar_"
 
#The suffix (or prefix) increment (or decrement) variable
counter = 1
 
#How many strings do you need? (specify a range, say:
#range(1,n+1) where n is the number of random numbers you need;
#OR range(0,n) where n is the number of random numbers you need.
myRange = range(0,100)
 
#print the strings using a for loop according to the specified range.
#define the suffix or prefix increment pattern as well
#If you want to add leading zeros, use the built-in function zfill:
#usage: string.zfill(final string length)
#Also see: 
#http://stackoverflow.com/questions/134934/display-number-with-leading-zeros
for count in myRange:
    print text + str(counter).zfill(3)
    counter += 1
#!/usr/bin/python

#print_incremental_data.py
#Author: Victor Miti
#Tested with python 2.6.8 and 2.7.5

'''-----------------------------Description--------------------------------'''
#Let us suppose that you have a string that you want to reproduce n times,
#with a prefix or suffix that increments (or decrements), for example:
#	String_001
#	String_002
#	String_003
#	String_004
#	String_... ,
#then this script will help you accomplish just that. Output is a text file
#You can modify the script accordingly if your incremental variable is in the
#middle of your text, for example:
#	foo_0001_bar
#	foo_0002_bar
#	foo_0003_bar
#	foo_0004_bar
#	foo_...._bar
'''------------------------------------------------------------------------'''

import sys, datetime

#get the current time
now = datetime.datetime.now()

#File Name Prefix
file_prefix = "textReproduction_"

#redirect stdout to a .txt file in the same directory
#we want the filename to have a timestamp
sys.stdout = open(file_prefix+now.strftime("%Y%m%d~%H%M%S")+".txt", "w")

#The text you want to reproduce n times
text = "Foobar_"

#The suffix (or prefix) increment (or decrement) variable
counter = 1

#How many strings do you need? (specify a range, say:
#range(1,n+1) where n is the number of random numbers you need;
#OR range(0,n) where n is the number of random numbers you need.
myRange = range(0,100)

#print the strings using a for loop according to the specified range.
#define the suffix or prefix increment pattern as well
#If you want to add leading zeros, use the built-in function zfill:
#usage: string.zfill(final string length)
#Also see: 
#http://stackoverflow.com/questions/134934/display-number-with-leading-zeros
for count in myRange:
    print text + str(counter).zfill(3)
    counter += 1

when I have time I will post the code which allows the generated data to be saved as a spreadsheet in .xls format.



Leave a Reply