Reputation: 43
I'm using pytrends
API to collect google trends data and save into a csv file. How do I automate writing the filename by taking the name from the list?
I tried the following method but it gives me a generator object
instead of ray ban or abaco
.
My code:
user_kw_list = ['ray ban','abaco sunglasses']
interest_over_time_df.to_csv("./data/{}_interest_over_time.csv".format(user_kw_list[i] for i in range(len(user_kw_list))), index=True)
Upvotes: 0
Views: 725
Reputation: 31
It depends if you want a long filename containing your entire user_kw_list
, or if you want to save a file for each keyword in your list.
In either case, f-strings may be worth looking into. If your version of Python supports it, it's very concise compared to the format method, and well-optimized. Read more about f-strings here.
If you want a long filename, you need to join
the items together. You almost certainly do not need a list comprehension in this case. Here's one possible solution:
user_kw_list = ['ray ban','abaco sunglasses']
joined = ('_or_').join(user_kw_list).replace(' ', '_')
# .replace(' ', '_') is optional. It maintains the snake_case naming convention.
path = f"./data/{joined}_interest_over_time.csv"
# This is functionally similar to: "./data/{}_interest_over_time.csv".format(joined)
interest_over_time_df.to_csv(path, index=True)
# This will save the file as ./data/ray_ban_or_abaco_sunglasses_interest_over_time.csv
# Note: Very long filenames can sometimes cause problems on some operating systems.
Conversely, if you want to save a file for each keyword in your list, the problem is that your loop is contained inside of the method call. It should be the other way around, with the for loop encapsulating the to_csv
method.
A traditional for-loop may be more readable for this use-case, since you're running code with side-effects, as opposed to returning a variable.
One solution would look like this:
user_kw_list = ['ray ban','abaco sunglasses']
for kw in user_kw_list:
kw = kw.replace(' ', '_')
# As before, this line is optional. It maintains the snake_case convention.
path = f"./data/{kw}_interest_over_time.csv"
# This is functionally similar to: "./data/{}_interest_over_time.csv".format(kw)
interest_over_time_df.to_csv(path, index=True)
# This will save two files: ray_ban_interest_over_time.csv
# and abaco_sunglasses_interest_over_time.csv
If the second case needs to be a list comprehension, that would look like this:
[interest_over_time_df.to_csv(f"./data/{kw}_interest_over_time.csv", index=True) for kw in user_kw_list]
Upvotes: 2