but I need to replace the inner quotes with 2 single quotes, like this: 'test''s','test','test''s more'. Has the cause of a rocket failure ever been mis-identified, such that another launch failed due to the same problem? mylist = [1,2,3]. I'm using python to write and read files.The data was stored in comma-separated text file. 565), Improving the copy in the close modal and post notices - 2023 edition, New blog post from our CEO Prashanth: Community is the future of AI. And if you're really looking to enclose the whole thing in single quotes: This may be easier than using string.replace. How a top-ranked engineering school reimagined CS curriculum (Ep. The eval is dangerous - you shouldn't execute user input. I have created this function to parse the list: Is there a neater way to achieve this? What is the difference between String and string in C#? Creating comma-separated string from list [duplicate], TypeError: sequence item 0: expected string, int found. Short story about swapping bodies as a job; the person who hires the main character misuses his body. I'd suggest using the csv module with StringIO. Obviously it gets more complicated if you need to quote/escape commas etc in the values. Looking for job perks? By clicking Post Your Answer, you agree to our terms of service, privacy policy and cookie policy. I have two stored sting variable I need to concat into a different variable for an api get request. What does "up to" mean in "is first up to launch"? Are double and single quotes interchangeable in JavaScript? rev2023.4.21.43403. How can I split this text properly in Python? Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide. Making statements based on opinion; back them up with references or personal experience. So using split (',') creates problems. Ignoring Commas in Quotes When Splitting a Comma-separated String In Python 2, it can be imported directly using the StringIO module. By clicking Post Your Answer, you agree to our terms of service, privacy policy and cookie policy. You need the double quotes on your string. The file 'test.csv' that the csv module produces: Note the quoting around "Header\n1" and "H, 4"` in the file so that the csv is correctly decoded in the future. How about saving the world? What would be your preferred way to concatenate strings from a sequence such that between every two consecutive pairs a comma is added. An example of such string "coffee", "water, hot" What I want to achieve ["coffee", "water, hot"] python Share Improve this question Using generator expressions has the benefit of also producing an iterator but saves importing itertools. Python list of strings to 1 string separated by comma & quotes (, & ") Asked 4 years, 3 months ago Modified 4 years, 3 months ago Viewed 825 times 0 I have a list of strings: list_of_strings = ["A", "List", "Of", "Strings"] I want to convert it to one new string looking like: new_string=""" "A", "List", "Of", "Strings" """ How about saving the world? What woodwind & brass instruments are most air efficient? Asking for help, clarification, or responding to other answers. The only new information here is a further processing step that is, How to convert string representation of list to a list, zen of python says flat is better than nested. It works with integers, strings (with 'crazy characters' as well) and it's just 1 line of code. My two cents. Method 1: Using str.split (',') If this is not what you wanted, the regexp needs to be souped up a bit. Find centralized, trusted content and collaborate around the technologies you use most. Each item is surrounded by quotes ("), but the items can also contain commas (,). Which was the first Sci-Fi story to predict obnoxious "robo calls"? Counting and finding real solutions of an equation. find Item wise max sales on which month in python series? If your lists can have more datatypes, or even contain lists within lists, then you will need a more complete grammar - like this one in the pyparsing examples directory, which will handle tuples, lists, ints, floats, and quoted strings. This should be applicable to lists with any length. By clicking Accept all cookies, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy. It's short and satisfies the requirements. For eg. Why does Acts not mention the deaths of Peter and Paul? What is the python "with" statement designed for? There are two ways to represent strings in python. Instead, you can use. Thanks for contributing an answer to Stack Overflow! Find centralized, trusted content and collaborate around the technologies you use most. Looking for job perks? Find centralized, trusted content and collaborate around the technologies you use most. Generate points along line, specifying the origin of point generation in QGIS. How to convert list lookalike into a list? Just some data with apostrophe is all I wanted. To learn more, see our tips on writing great answers. "Signpost" puzzle from Tatham's collection. Has the cause of a rocket failure ever been mis-identified, such that another launch failed due to the same problem? Stepwise explanation: Define a regular expression pattern that matches commas followed by any number of spaces (",\s*"). Examples: Input: List<String> = ["Geeks", "ForGeeks", "GeeksForGeeks"] Output: "Geeks, For, Geeks" Input: List<String> = ["G", "e", "e", "k", "s"] Output: "G, e, e, k, s" Approach: This can be achieved with the help of join () method of String as follows.