csv file
csv file
Hello and thank you for your forum
i try this code on esp12 :
dat = []
with open("chauf.txt") as file:
for line in file.readlines():
liste = [int(c) for c in line.strip().split(",")]
dat.append(liste)
with file 'chauf.txt' :
7,45,16,16,16,16,16,18,18
9,0,16,16,16,16,16,16,16
19,0,18,18,18,18,18,16,16
20,0,18,18,18,18,18,18,18
on linux python 2.7 i have : dat[0][0] = 7
on esp12 = nothing
i no understand
can you help please
thank
sorry for my english
i try this code on esp12 :
dat = []
with open("chauf.txt") as file:
for line in file.readlines():
liste = [int(c) for c in line.strip().split(",")]
dat.append(liste)
with file 'chauf.txt' :
7,45,16,16,16,16,16,18,18
9,0,16,16,16,16,16,16,16
19,0,18,18,18,18,18,16,16
20,0,18,18,18,18,18,18,18
on linux python 2.7 i have : dat[0][0] = 7
on esp12 = nothing
i no understand
can you help please
thank
sorry for my english
Re: csv file
i re -try and error
exec(open('./fichier.py').read(),globals())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 5, in <module>
File "<string>", line 4, in <module>
File "<string>", line 4, in <listcomp>
ValueError: invalid syntax for integer with base 10
dat=[]
with open('chauf.txt') as file:
for line in file.readlines():
liste = [int(c) for c in line.strip().split(",")]
dat.append(liste)
exec(open('./fichier.py').read(),globals())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 5, in <module>
File "<string>", line 4, in <module>
File "<string>", line 4, in <listcomp>
ValueError: invalid syntax for integer with base 10
dat=[]
with open('chauf.txt') as file:
for line in file.readlines():
liste = [int(c) for c in line.strip().split(",")]
dat.append(liste)
Re: csv file
This likely comes from int(c) -- it means that whatever is in your CSV file isn't numeric. Or perhaps you have just one cell that isn't a number.
Code: Select all
>>> int('hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid syntax for integer with base 10: 'hello'
Re: csv file
Note: an empty string will cause the same problem.
Also, in Python (perhaps confusingly, but makes sense when you think about), splitting the empty string returns [''], so a blank line in your CSV would cause the same problem.
Also, in Python (perhaps confusingly, but makes sense when you think about), splitting the empty string returns [''], so a blank line in your CSV would cause the same problem.
Re: csv file
The error indicates extra character s in the file, which are not stripped, maybe \r
Re: csv file
thank you for your answer ,
i see no 'blank' or no numeric
i try with in file
7,45,16
8,50,20
it's same
possible with '\r' but where put in code
thank
i see no 'blank' or no numeric
i try with in file
7,45,16
8,50,20
it's same
possible with '\r' but where put in code
thank
Re: csv file
Just verified that: an empty line at the end will cause that error too. SO better test for empty lines before converting.Note: an empty string will cause the same problem.
Re: csv file
with notpad++
7,45,16[CR][LF]
9,50,20[CR][LF]
with code
[['7', '45', '16\r9', '50', '20']]
7,45,16[CR][LF]
9,50,20[CR][LF]
with code
Code: Select all
dat=[]
with open('chauf.txt') as file:
for line in file.readlines():
print(line)
liste = line.strip().split(",")
dat.append(liste)
[['7', '45', '16\r9', '50', '20']]
Re: csv file
What does the output of your program show? (i.e. what does print(line) show).
The only way I can imagine that you'd end up with [['7', '45', '16\r9', '50', '20']] is if the file _only_ had CR (i.e. MacOS 9 style).
Can you try running this and see what the output is:
The only way I can imagine that you'd end up with [['7', '45', '16\r9', '50', '20']] is if the file _only_ had CR (i.e. MacOS 9 style).
Can you try running this and see what the output is:
Code: Select all
f = open('chauf.txt')
print(f.read())
Re: csv file
hi,
Code: Select all
>>> f = open('chauf.txt')
>>> print(f.read())
7,45,16
9,50,20
>>>