Dec-13-2022, 07:39 PM
(This post was last modified: Dec-13-2022, 07:39 PM by deanhystad.)
skipinitialspace=True is required if you have spaces after your separator character
The newline='' in:
I would read the file like this:
Output: separator
v
CompanyName LTD, "[email protected],[email protected]"
^
whitespace
If you don't skip the initial space(s) the csv reader misses the starting quote which messes up parsing the rest of the line. Instead of three columns like this.Output:CompanyName LTD
[email protected], [email protected]"
12234,56678
you end up with 5Output:CompanyName LTD
"[email protected]
[email protected]"
"12234
56678"
Such a subtle and not obvious difference that has a big effect.The newline='' in:
with open("my_magic.csv", "r", newline="") as f:removes newline characters (\n) from the end of lines read from the file. I don't think this is really needed here. The csv reader ignores the newline characters. Setting the newline to '' when writing a csv file prevents the csv writer writing a blank line after each row(two newline characters at the end of each row). I think that only happens on Windows
I would read the file like this:
import csv with open("test.csv", "r") as file: data = csv.reader(file, skipinitialspace=True) for company, emails, numbers, in data: emails = emails.split(",") numbers = numbers.split(",") print(company, emails, numbers)