csv file

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
bellad
Posts: 96
Joined: Tue May 14, 2019 1:47 pm

csv file

Post by bellad » Tue May 14, 2019 1:53 pm

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

bellad
Posts: 96
Joined: Tue May 14, 2019 1:47 pm

Re: csv file

Post by bellad » Wed May 15, 2019 7:15 am

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)

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: csv file

Post by jimmo » Wed May 15, 2019 7:25 am

bellad wrote:
Wed May 15, 2019 7:15 am
ValueError: invalid syntax for integer with base 10
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'

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: csv file

Post by jimmo » Wed May 15, 2019 7:28 am

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.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: csv file

Post by Roberthh » Wed May 15, 2019 7:52 am

The error indicates extra character s in the file, which are not stripped, maybe \r

bellad
Posts: 96
Joined: Tue May 14, 2019 1:47 pm

Re: csv file

Post by bellad » Wed May 15, 2019 8:17 am

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

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: csv file

Post by Roberthh » Wed May 15, 2019 8:21 am

Note: an empty string will cause the same problem.
Just verified that: an empty line at the end will cause that error too. SO better test for empty lines before converting.

bellad
Posts: 96
Joined: Tue May 14, 2019 1:47 pm

Re: csv file

Post by bellad » Wed May 15, 2019 8:53 am

with notpad++

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']]

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: csv file

Post by jimmo » Wed May 15, 2019 10:12 am

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:

Code: Select all

f = open('chauf.txt')
print(f.read())

bellad
Posts: 96
Joined: Tue May 14, 2019 1:47 pm

Re: csv file

Post by bellad » Thu May 16, 2019 7:01 am

hi,

Code: Select all

>>> f = open('chauf.txt')
>>> print(f.read())
7,45,16
9,50,20

>>> 

Post Reply