![]() |
|
[Tut] Python Convert String to CSV File - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Convert String to CSV File (/thread-99746.html) |
[Tut] Python Convert String to CSV File - xSicKxBot - 07-26-2022 Python Convert String to CSV File 5/5 – (1 vote) Problem Formulation Given a Python string: Code: my_string = '''a,b,c
1,2,3
9,8,7''' Question: How to convert the string to a CSV file in Python?The desired output is the CSV file: Code: 'my_file.csv'Code: a,b,c
1,2,3
9,8,7To convert a multi-line string with comma-separated values to a CSV file in Python, simply write the string in a file (e.g., with the name Code: 'my_file.csv'This works if the string is already in the correct CSV format with values separated by commas. The following code uses the Code: open()Code: file.write()Code: my_string = '''a,b,c
1,2,3
9,8,7''' with open('my_file.csv', 'w') as out: out.write(my_string)The result is a file Code: 'my_file.csv'Code: a,b,c
1,2,3
9,8,7The string may not be in the correct CSV format. For example, you may want to convert any of the following strings to a CSV file—their format is not yet ready for writing it directly in a comma-separated file (CSV):
To parse such a string and modify it before writing it in a file Code: 'my_file.csv'Code: string.replace()Code: string.split()Let’s go over each of those examples to see how to parse the string effectively to bring it into the CSV format: Example 1 Code: # Example 1:
my_string = 'abc;123;987' with open('my_file.csv', 'w') as out: lines = [','.join(line) for line in my_string.split(';')] my_string = '\n'.join(lines) out.write(my_string)I’ve higlighted the two code lines that convert the string to the CSV format.
The output file Code: 'my_file.csv'Code: a,b,c
1,2,3
9,8,7The following example is the same as the previous code snippet, only that the empty spaces Code: ' 'Code: # Example 2:
my_string = 'abc 123 987' with open('my_file.csv', 'w') as out: lines = [','.join(line) for line in my_string.split(' ')] my_string = '\n'.join(lines) out.write(my_string)The output file Code: 'my_file.csv'Code: a,b,c
1,2,3
9,8,7If the comma-separated values are not yet comma-separated (e.g., they may be semicolon-separated Code: 'a;b;c'Code: string.replace()This is shown in the following example: Code: # Example 3:
my_string = 'a=b=c 1=2=3 9=8=7' with open('my_file.csv', 'w') as out: my_string = my_string.replace('=', ',').replace(' ', '\n') out.write(my_string)Thanks for reading this article! I appreciate the time you took to learn Python with me. If you’re interested in writing more concise code, feel free to check out my one-liner book here: Python One-Liners Book: Master the Single Line First! Python programmers will improve their computer science skills with these useful one-liners. ![]() Python One-Linerswill teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert. The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms. Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments. You’ll also learn how to:
By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line. Get your Python One-Liners on Amazon!! |