简体   繁体   English

Python文件路径在pycharm正则表达式混淆中失败

[英]Python file path failing in pycharm regex confusion

I am a relatively new python user and am getting a funky error using my IDE (pycharm), but not when using the cmd line. 我是一个相对较新的python用户,使用我的IDE(pycharm)得到一个时髦的错误,但是在使用cmd行时没有。

Simply I: 我只是:

path ='C:\Users\Dell\Downloads\users.dat'

import pandas as pd
unames = ['user_id', 'gender', 'age', 'occupation', 'zip']
users = pd.read_table(path, sep='::', header=None, names=unames)

After which I receive an error that indicates: 之后我收到一个错误,表明:

ParserWarning: Falling back to the 'python' engine because the 'c' engine does not 
support regex separators; you can avoid this warning by specifying engine='python'.
ParserWarning)

When i input the identical commands into the cmd line and print users the data prints as expected (ie, no errors or anything funky). 当我将相同的命令输入到cmd行并打印users ,数据按预期打印(即没有错误或任何时髦)。

EDIT: similarly when I input 编辑:同样当我输入

ratingsdata ='C:\Users\Dell\Downloads\ratings.dat'

I get a funky IOError: [Errno 22] invalid mode ('r') or filename: Not sure as to why the /r is not ok in a file path... I understand it's regex, but within the quoted lines?? 我得到一个时髦的IOError: [Errno 22] invalid mode ('r') or filename:不确定为什么/r在文件路径中不正确...我理解它是正则表达式,但在引用行内?

Help! 救命!

Looks like on Python 2.7 Pandas just doesn't handle separators that look regexish. 看起来像 Python 2.7 Pandas只是不处理看起来像 regexish的分隔符。 The initial "error" can be worked around by adding engine='python' as a named parameter in the call, as suggested in the warning. 如警告中所示,可以通过在调用中添加engine='python'作为命名参数来解决初始“错误”。

Looks like you are attempting to do an exercise from the "Python For Data Analysis" book, I ran into this same issue. 看起来你正试图从“Python For Data Analysis”一书中做一个练习,我遇到了同样的问题。 All you have to do is use double slashes instead of single slashes like so: 您所要做的就是使用双斜杠而不是单斜杠,如下所示:

path ='C:\\\\Users\\\\Dell\\\\Downloads\\\\ratings.dat'

如果您正在使用“Python for Data Analysis”并使用PyCharm和Python 3.x,那么可以在此处指定引擎:

ratings = pd.read_table(path + '/ratings.dat', sep='::', header=None, names=rnames, engine='python')

You can add engine = 'python' as a parameter just before the end bracket. 您可以在end括号之前添加engine ='python'作为参数。

For Example: 例如:

ratings = pd.read_table('.../iPythonNotebooks/1mil_movie_reco/ml-1m/ratings.dat', sep='::', header=None, names=rnames, engine = 'python') ratings = pd.read_table('... / iPythonNotebooks / 1mil_movie_reco / ml-1m / ratings.dat',sep ='::',header = None,names = rnames,engine ='python')

I had a similar problem in El Capitan using Python 3. My csv file had a space after all the separators (;) so I used the delimiter '; 我在使用Python 3的El Capitan中遇到了类似的问题。我的csv文件在所有分隔符(;)之后有一个空格所以我使用了分隔符'; ' (with a space) in the read_csv call, thus I received the warning. 在read_csv调用中'(带空格),因此我收到了警告。

So I deleted all spaces from the csv file (which was comprised exclusively of integers) and the warning disappeared. 所以我删除了csv文件中的所有空格(仅由整数组成),警告消失了。 Then I added a third column with a header named "some text" without the quotes and data also with spaces. 然后我添加了第三列,标题为“some text”,没有引号,数据也带有空格。 I had to call the column using ['col_name'] so the print function could do its job 我不得不使用['col_name']调用列,因此print函数可以完成它的工作

dataFile = pd.read_csv("dude.csv", sep = ';')
col2 = dataFile.col2
col3 = dataFile['some text']
print(col2)
print(col3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM