[Tutor] Field/Variable References
Serdar Tumgoren
zstumgoren at gmail.com
Fri Dec 18 17:52:43 CET 2009
I'm reposting John's follow-up question (below) to the list.
> Thank you - can you please assume that the data provided is the following:
>
> Columns 1 - 4 = Name (in record 1 of example = "John")
> Column 5 = Answer1 (in record 1 of example = "9")
> Column 6 = Answer2 (in record 1 of example = "8")
> Column 7 = AreaCode (in record 1 of example = "762")
>
> Sorry - I am new to Python and trying to see if I can do data manipulation
> in the same detail that I am used to in the SAS Data Step.
>
In the above case, the simplest solution might be to use Python's
slicing syntax. The below solution assumes that the name can vary in
number of characters, but that the digits for your Answers and area
code do not vary (in other words, Answer1 and Answer2 are always a
single digit, and AreaCode is always three digits).
for line in open('my_data_file.txt'):
name = line[0:-5] #start of line up to 5 places before end of
string; zero-indexed with non-inclusive endpoint
Answer1 = line[-5] #grab fifth digit from end of line
Answer2 = line[-4] #grab fourth digit from end of line
AreaCode = line[-3:] #grab from 3 places before end of line, through the end
# do stuff with name, Answer1, Answer2, AreaCode
The slicing syntax can be tricky because the index varies depending on
which direction you're counting from. If you're counting start to end,
it's zero-indexed. In other words, the first "column" or character in
the string is numbered 0 (so line[0] will give you the letter "J"). On
the other hand, if you're counting from end-of-string to beginning,
the characters are indexed starting with -1. So the "first" character
from the right (represented as -1) will be 2.
Others on the list can probably explain it better than me. Meantime,
you might want to check out this intro page, giving special focus to
the sections on strings and lists.
http://docs.python.org/tutorial/introduction.html
HTH!
Serdar
More information about the Tutor
mailing list