Is there a quick way to put quotation marks around strings in an array?

AI Thread Summary
The discussion centers on efficiently converting a list of words into an array with quotation marks around each word. Participants suggest various methods based on the programming language in use. In Python, a simple approach involves splitting a string of words and then modifying the list comprehension to add quotes around each word. For those using comma-delimited lists, importing the data into a word processor to replace commas with quotes is recommended, though caution is advised to avoid "smart quotes." The conversation also touches on whether the task is a one-time need or if it will be repeated, influencing whether the words should be hardcoded or stored in a separate file for dynamic access. Ultimately, the easiest solution was highlighted, emphasizing flexibility in choosing tools beyond the initially considered options.
Darkmisc
Messages
222
Reaction score
31
TL;DR Summary
I'd like to cut and paste a list of words into an array. I'll need to put quotation marks around them all. Is there a way to do this other than to go through each word one by one?
Hi everyone

I'd like to cut and paste a list of words into an array. I'll need to put quotation marks around them all. Is there a way to do this other than to go through each word one by one?

Thanks
 
Technology news on Phys.org
You could make one long string and then parse it to get the words.

Depends on the language you're using.

As an example, in Python:
Python:
s="aaa bbb ccc ddd eee fff"
d=s.split(" ")
print(d)

and you'll see:
Code:
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
 
  • Like
Likes FactChecker and Darkmisc
Is your list comma-deliminted? If so, import it into a word processor that can handle text files and replace all commas with quote-comma-quote. Fix the first and last entry and you';re done.
 
jedishrfu said:
You could make one long string and then parse it to get the words.

Depends on the language you're using.

As an example, in Python:
Python:
s="aaa bbb ccc ddd eee fff"
d=s.split(" ")
print(d)

and you'll see:
Code:
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
This code is basically just splitting the words. The OP also says that they want quotation marks around each word. I presume that when the OP parses that list of words, for each element in that list, they would want the character at positions 0 and -1 to be '. This can be achieved by a simple modification of your code:
Python:
>>> d = ["\'" + word + "\'" for word in s.split(" ")]
>>> d
["'aaa'", "'bbb'", "'ccc'", "'ddd'", "'eee'", "'fff'"]
>>> (d[0])[0]
"'"
>>> (d[0])[-1]
"'"
 
Vanadium 50 said:
Is your list comma-deliminted? If so, import it into a word processor that can handle text files and replace all commas with quote-comma-quote. Fix the first and last entry and you';re done.
Some care is needed with word processors, which sometimes replace quotes with the better looking "smart quotes". But I'd do similar with :s/,/","/ in vi. 😁
 
Ibix said:
in vi.
I was going to suggest sed. Usually sed won't solve your problems - but it will put them in perspective.
 
Is this something that needs to be done just once, or many times?

Do you want to prepare an array declaration statement in a program, using a fixed set of words? Then you run the program repeatedly using the same set of words. Maybe update the set of words only occasionally, such that editing the code is not a problem.

Or are you going to use a different set of words every time you run the program? In that case, you'd probably want to have the words in a separate file that the program reads in order to generate the array.
 
jtbell said:
Is this something that needs to be done just once, or many times?

Do you want to prepare an array declaration statement in a program, using a fixed set of words? Then you run the program repeatedly using the same set of words. Maybe update the set of words only occasionally, such that editing the code is not a problem.

Or are you going to use a different set of words every time you run the program? In that case, you'd probably want to have the words in a separate file that the program reads in order to generate the array.
For now, it's just once. Easiest method was Vanadium50's. I'd completely forgotten that I could use something other than Godot to solve the problem.
 
Back
Top