Python / Pandas - error reading Arq. csv ( path)

Asked

Viewed 573 times

-1

I’m using Google Colaboratory and trying to read a CSV file, separated by ";" and is giving the following error. I think it has to do with the path.

What I’m doing wrong?

import pandas as pd

path_file = "D:\##-001-RedeDBServer_NT1\09-NinjaTrader\00 - Logs DB\Log_1500_StepFin.csv"
log = pd.read_csv(path_file, sep = ';')

Error:

ValueError                                Traceback (most recent call last)
<ipython-input-1-9e17d271e0b4> in <module>()
      6 
      7 
----> 8 log = pd.read_csv(path_file, sep = ';')

5 frames
pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader.__cinit__()

pandas/_libs/parsers.pyx in pandas._libs.parsers.TextReader._setup_parser_source()

/usr/lib/python3.6/genericpath.py in exists(path)
     17     """Test whether a path exists.  Returns False for broken symbolic links"""
     18     try:
---> 19         os.stat(path)
     20     except OSError:
     21         return False

ValueError: stat: embedded null character in path
  • Try adding the prefix r in his path: path_file = r"..."

  • Unfortunately it did not work. Returns the same error.

2 answers

4


Trade in all your "\" for "\\", or by "/".

  • the problem is that the character " " is used in programming languages, such as C or Python to indicate a special character in a string, and only if the character that comes after " " does not compose a special sequence is that a " " is understood as a character.

This is because when Microsoft released DOS in 1981, which was supposed to be similar to existing Unix and CP/M, for some reason it decided to use " to separate directories, instead of the "/" that was already used on other systems. Maybe just to "be a little different". But the C language that gained popularity and was the syntax model for dozens of other programming languages already used the " " for something else. Almost 40 years later, everyone pays the price for this "let’s do it differently" moment.

In your specific case, you have the sequence \00, that the Python language translates to a byte of value "0", which is used in the OS file Apis to indicate the end of a string (the name would be truncated. If it were C, this same string would compile without error, and simply it would never find the file because it would see the name only until the occurrence of \00). Put \\00 ( and \\ in all other \) that works. Python prefix usage r" for strings improves this by turning off almost all special sequences starting with " " - but does not turn off all. Type "\\" converts the two bars to a single one internally and always works - and Python still translates "/" automatically to directory separation, even when the program is in Windows - so it would also work.

(Even Python in the near future is changing this to give error while reading the code, instead of letting the error happen when the program is running and trying to access the file using an invalid path: unknown " sequences will give error, instead of leaving the " " untouched).

(Note that the use of \ how exhaust is so widespread that so far in stackoverflow, if you type \\ without marking it as code, it condenses into a single \: "\" <- there are two bars there. )

0

The problem continues. I changed the directory where the file is stored, I changed the name of the tbm file, but I still can’t load the file.


import pandas as pd

path_file = r’D:/Testelog/Log1500.txt'

log = pd.read_csv(path_file, delimiter=';')


Filenotfounderror Traceback (Most recent call last) in () 3 path_file = r’D:/Testelog/Log1500.txt' 4 -----> 5 log = pd.read_csv(path_file, delimiter=';')

4 frames /usr/local/lib/python3.6/dist-Packages/pandas/io/parsers.py in init(self, src, **kwds) 1915 kwds["usecols"] = self.usecols 1916 -> 1917 self. _Reader = parsers.Textreader(src, **kwds) 1918 self.unnamed_cols = self. _Reader.unnamed_cols 1919

pandas/_libs/parsers.pyx in pandas. _libs.parsers.Textreader.cinit()

pandas/_libs/parsers.pyx in pandas. _libs.parsers.Textreader. _setup_parser_source()

Filenotfounderror: [Errno 2] File b’D:/Testelog/Log1500.txt' does not exist: b’D:/Testelog/Log1500.txt'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.