I am reading a csv file and I use a split on every line but I do not fully understand the behavior.
csv file:
JavaScript
x
4
1
id;name;number
2
1111;foo nbar;2.00
3
2222;a 0,25;1.00
4
code:
JavaScript
1
9
1
for file in csvs:
2
with open(file, encoding = "utf8") as f:
3
reader = csv.reader(f)
4
for row in reader:
5
if isinstance(row, str):
6
print(row)
7
else:
8
print(row[0].split(";"))
9
This yields:
JavaScript
1
4
1
['number', 'name', 'price']
2
['1111', 'foo \nbar', '2.00']
3
['2222', 'a 0']
4
but I expected the last line to be:
JavaScript
1
2
1
['2222', 'a 0,25', '1.00']
2
Advertisement
Answer
Use the delimiter arg to csv.reader
to split on semicolons instead of commas:
JavaScript
1
6
1
for file in csvs:
2
with open(file, encoding = "utf8") as f:
3
reader = csv.reader(f,delimiter=";")
4
for row in reader:
5
print(row)
6