I have a list of IDs I'm trying to manipulate in order to get long strings of the following format:
"ID1,ID2,ID3,....,IDn".
that is, a long string where the IDs are separated by commas, which are also a part of the string.
Here's the catch: each of these long strings can be no more than 1,000
characters in length total, and since I want the code to work on any list
I feed it, I can't say in advance how many strings of length 1,000
the raw list of IDs will require.
So, ideally I'd like to have a script that takes that list of IDs and generates variables of the following form:
str1 = string of the first 1000 chars
str2 = string of the next 1000 chars
srt3 = string of the next 1000 chars
and so on.
How do I do that? How do I generate variables on the go by need?
I thought about maybe generating one long string at the first stage, like:
long_str = ""
for item in my_list:
long_str += str(item) + ","
def find_num_segments(x):
if x % 1000 == 0:
return x/1000
else:
return x/1000 + 1 # notice that if x < 1000, then x%1000 != 0 (actually == x). So it will fall under else. x<1000 / 1000 gives 0, so the function yields 0+1
num_segments = find_num_segments(long_str)
for i in range(num_segments):
starting_position = 0
print "str%d : " %i , num_segments[starting_position,starting_position+1000]
starting_position += 1000
BUT, that's:
- Ugly and probably unpythonic.
- Doesn't produce clean results as there are "formatting leftovers" such as the brackets and quotes -
['014300070358Ful'], ['014300031032Uni']
. - Doesn't actually work :) I get a
"TypeError: not all arguments converted during string formatting"
on the 1st line offind_num_segments()
.
EDIT: I realize that won't produce the correct outcome either, as there's no guarantee an id won't be "cut" in the middle.
How would you create a function that concatenates the IDs one by one and "stops" before getting to the 1000 chars mark if the next ID won't fit all the way in, then starts a new batch of 1000 chars?
Here's a sample list if anybody wants to help and needs one.
Help would be appreciated! Thanks :)