Created
November 25, 2020 06:24
-
-
Save warabanshi/fe1ab326326459d852ce1d39203fd937 to your computer and use it in GitHub Desktop.
read CSV string which includeds line break in a row through csv.reader()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv, io | |
text = """ | |
12345,12345,revise,one line comment | |
12345,13245,revise,"State officials were flying over southeastern Utah looking for sheep as part of a routine task. Instead they found something straight out of a sci-fi movie. | |
From a helicopter, officers from the Utah Department of Public Safety spotted a large metal monolith — a single block of metal — last week. It was sitting in Utah's Red Rock Country in the southeast. Officials have no idea how or when it got there — or who might have placed it." | |
""".strip() | |
def output_lines(c1, c2, c3, c4): | |
print(f'id={c1}, comment={c4}') | |
contents = csv.reader(text.splitlines()) | |
[output_lines(*row) for row in contents] | |
contents = csv.reader(io.StringIO(text)) | |
[output_lines(*row) for row in contents] | |
""" output | |
[[ by text.splitlines() ]] | |
id=12345, comment=one line comment | |
id=12345, comment=State officials were flying over southeastern Utah looking for sheep as part of a routine task. Instead they found something straight out of a sci-fi movie.From a helicopter, officers from the Utah Department of Public Safety spotted a large metal monolith — a single block of metal — last week. It was sitting in Utah's Red Rock Country in the southeast. Officials have no idea how or when it got there — or who might have placed it. | |
[[ by io.StringIO(text). comment keeps line breaks ]] | |
id=12345, comment=one line comment | |
id=12345, comment=State officials were flying over southeastern Utah looking for sheep as part of a routine task. Instead they found something straight out of a sci-fi movie. | |
From a helicopter, officers from the Utah Department of Public Safety spotted a large metal monolith — a single block of metal — last week. It was sitting in Utah's Red Rock Country in the southeast. Officials have no idea how or when it got there — or who might have placed it. | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment