I started to learn pandas by following this tutorial:
https://github.com/jvns/pandas-cookbook
Right in the first chapter I try very elementary example of reading a csv file. The example goes like this:
import pandas as pd
broken_df = pd.read_csv("..\data\bikes.csv")
I get a lengthy error message, which ends with a line:
FileNotFoundError: File b'..\\data\x08ikes.csv' does not exist
So although I write 'bikes.csv', which I have in the correct folder, the program seems to be searching for a file called 'x08ikes.csv'. Could this be an encoding error? sys.getdefaultencoding() returns 'utf-8'.
I am using Anaconda3 for 64bit Windows, version 4.4.0. My browser is Brave. Any ideas what is going wrong here?
The backslash character '\' has special meaning; it tries to "escape" the next character. In this case '\b' is an escape character that does have a meaning. There are three ways around this:
Escape the escapes:
You can use the backslash to escape the next backslash, telling Python "this is just another character"
broken_df = pd.read_csv("..\\data\\bikes.csv")
Use a raw string:
Placing r at the beginning of a string tells Python to interpret everything in the string as-is
broken_df = pd.read_csv(r"..\data\bikes.csv")
Use forward slashes:
This is specific to file paths. You can trace the directory to you file using forward slashes instead of backslashes.
broken_df = pd.read_csv("../data/bikes.csv")
What you can do is, upload the bikes.csv in to the Jupyter Home "Files" tab. Open it and you may still see the message. Then go to File->New, and you may get a new blank file. Open the original bikes.csv in notepad, copy and paste the content in to the file in jupyter notebook. This may help to resolve it.
Then you can run the following code.
import pandas as pd
broken_df = pd.read_csv("..\data\bikes.csv")
Related
I am trying to load an excel file within an IronPython script, which is embedded within a software.
I tried to do this with the following code:
import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
import Microsoft.Office.Interop.Excel as Excel
excel = Excel.ApplicationClass()
excel.Visible = True
workbook = excel.Workbooks.Open("C:\Users\antoi\Desktop\rimo_report\ae-project-reporting\codes_02_heat_recovery\data\unit_data_simp_08.xlsx")
However, I get an error that the file path cannot be found. From the error, it seems it is not searching within my own computer. How could one do this ? Do I have to import other packages ?
if it's just a path error, try to escape your \.
\ is a character used to escape some other characters (such as quotes, new lines, etc)
So when python is reading your code and interpreting it, it is trying to understand the characters you escaped such as \U, \a ...
To avoid this problem you can try this :
workbook = excel.Workbooks.Open("C:\\Users\\antoi\\Desktop\\rimo_report\\ae-project-reporting\\codes_02_heat_recovery\\data\\unit_data_simp_08.xlsx")
I've been working on a program that reads out an specific PDF and converts the data to an Excel file. The program itself already works, but while trying to refine some aspects I ran into a problem. What happens is the modules I'm working with read directories with simple slashes dividing each folder, such as:
"C:/Users/UserX"
While windows directories are divided by backslashes, such as:
"C:\Users\UserX"
I thought using a simple replace would work just fine:
directory.replace("\" ,"/")
But whenever I try to run the program, the \ isn't identified as a string. Instead it pops up as orange in the IDE I'm working with (PyCharm). Is there anyway to remediate this? Or maybe another useful solution?
In general you should work with the os.path package here.
os.getcwd() gives you the current directory, you can add a subfolder of it via more arguments, and put the filename last.
import os
path_to_file = os.path.join(os.getcwd(), "childFolder", filename)
In Python, the '\' character is represented by '\\':
directory.replace("\\" ,"/")
Just try adding another backslash.
First of all you need to pass "C:\Users\UserX" as a raw string. Use
directory=r"C:\Users\UserX"
Secondly, suppress the backslash using a second backslash.
directory.replace("\\" ,"/")
All of this is required as in python the backslash (\) is a special character known as an escape character.
Try this:
import os
path = "C:\\temp\myFolder\example\\"
newPath = path.replace(os.sep, '/')
print(newPath)
Output:<< C:/temp/myFolder/example/ >>
here is my simple code of python that I wrote to save the screenshot of a webpage.
from selenium import webdriver
import time
driver=webdriver.Firefox()
driver.get("https://www.google.co.in")
driver.implicitly_wait(2)
driver.save_screenshot("D\amanulla\test.png")
driver.quit()
Though, the program running without any errors, I do not see any screenshots saved on my machine. Can someone help me on this?
You are missing : from "D\amanulla\test.png" and you need to escape the \ as well, so effectively the line will be either:
"D:\\amanulla\\test.png"
or
"D:/amanulla/test.png"
I do not see any screenshots saved on my machine
Look for a file named Dmanulla est.png in the default Downloads location for your browser... because that is what you are instructing WebDriver to do with the line:
driver.save_screenshot("D\amanulla\test.png")
Explanation:
The string "D\amanulla\test.png" will be interpreted as "Dmanulla est.png". This is because backslashes are escape sequences within Python strings. Your directory separators will be interpreted as \a (bell) and \t (tab).
Also, the : separator is missing between the drive letter and the file path, so it is treating the entire string as a filename. In the absence of a directory name, it should save to browser's default "Downloads" directory.
Solution:
driver.save_screenshot(r"D:\amanulla\test.png")
This uses a raw string so the backslashes are not interpreted as escape sequences, and it adds the missing : as the drive letter separator.
I am using Python 3.4.2 on Windows 10 and am just getting into opening and read/writing to files from the Python shell
I did this test and got the following error message in spite of the fact that I had created the file beforehand (but not from the shell as that would not work either).
Can someone tell me what I haven't taken into consideration here because all my searches tell me this should work.
>>> import os
>>> helloFile = open('C:\\Users\\jennifer\\Hello.txt')
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
helloFile = open('C:\\Users\\jennifer\\Hello.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\jennifer\\Hello.txt'
I did as John Gordon suggested and removed .txt from the pathname and it still didn't work.
Here is the directory and path for the file:
C:\Users\jennifer\Desktop\Hello
Finally, it has opened and will be very aware of the need to call the complete path in the future! Thank you
Since you have backslashes (\) in the string and since backslashes in string literals mean that a special character is denoted (like \n for newline), you need to make clear that you want verbatim backslashes (as needed by Windows file paths).
For this you have two major options:
You can escape the backslashes with an additional backslash, i. e. effectively double each backslash: 'C:\\foo\\bar'
You can prepend an r to the string to declare it a regexp string in which the special meaning of the backslash is suspended: r'C:\foo\bar'.
There are more options but these are the two major ones used.
Beware, however, that the second option suffers a wart in the Python parser which prevents it to denote a backslash at the end of the string. So if you ever want to have a string literal ending in a backslash (e. g. C:\foo\bar\), then you cannot use this option.
You only need to put a '\' after the Drive, this is to avoid a unicode error. Code below is assuming you have already run Python in Powershell (Window's CMD).
Also if you want to read the file (i.e. print the contents in the CMD) you will need to make it readable by putting r before the filepath.
file =open(r,'C:\\Users\jennifer\Desktop\Hello.txt')
To print the contents:
for i in file:
print(i)
Then hit Enter twice to get your output.
I am trying to read a txt file(kept in another location) in python, but getting error.
FileNotFoundError
in ()
----> 1 employeeFile=open("C:/Users/xxxxxxxx/Desktop/python/files/employee.txt","r")
2 print(employeeFile.read())
3 employeeFile.close()
FileNotFoundError: [Errno 2] No such file or
directory:'C:\u202a/Users/xxxxxxxx/Desktop/python/files/employee.txt'
Code used:
employeeFile=open("C:/Users/xxxxxxxx/Desktop/python/files/employee.txt","r")
print(employeeFile.read())
employeeFile.close()
I tried using frontslash(/) and backslash(). But getting the same error.Please let me know what is missing in code.
I'm guessing you copy and pasted from a Windows property pane, switching backslashes to forward slashes manually. Problem is, the properties dialog shoves a Unicode LEFT-TO-RIGHT EMBEDDING character into the path so the display is consistent, even in locales with right-to-left languages (e.g. Arabic, Hebrew).
You can read more about this on Raymond Chen's blog, The Old New Thing. The solution is to delete that invisible character from your path string. Selecting everything from the initial " to the first forward slash, deleting it, then retyping "C:/, should do the trick.
As your error message suggests, there's a weird character between the colon and the forward slash (C:[some character]/). Other than that the code is fine.
employeeFile = open("C:/Users/xxxxxxxx/Desktop/python/files/employee.txt", "r")
You can copy paste this code and use it.