简体   繁体   English

读取 Python 中的 CSV 文件:意外标记 `(' 附近的语法错误

[英]Read CSV File in Python: Syntax error near unexpected token `('

df = pd.read_csv( "https://raw.githubusercontent.com/Apress/data-analysis-and-visualization-using-python/master/Ch07/Salaries.csv")

The error I get is: bash: syntax error near unexpected token `('我得到的错误是: bash: 意外标记附近的语法错误 `('

When I switch it to [] is get:当我将它切换到 [] 时得到:

df = pd.read_csv["https://raw.githubusercontent.com/Apress/data-analysis-and-visualization-using-python/master/Ch07/Salaries.csv"]

Filesystem           1K-blocks      Used Available Use% Mounted on
df: =: can't find mount point

I'm unsure if I'm missing code or entering it incorrectly.我不确定我是否缺少代码或输入错误。

You're trying to run python in the shell instead of in python.您正在尝试在 shell 中而不是在 python 中运行 python。

Within the shell enter py , python or python3 .在 shell 中输入pypythonpython3

From there, do your normal python commands:从那里,执行正常的 python 命令:

import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/Apress/data-analysis-and-visualization-using-python/master/Ch07/Salaries.csv")

Produces生产

>>> df
         rank discipline  phd  service     sex  salary
0        Prof          B   56       49    Male  186960
1        Prof          A   12        6    Male   93000
2        Prof          A   23       20    Male  110515
3        Prof          A   40       31    Male  131205
4        Prof          B   20       18    Male  104800
..        ...        ...  ...      ...     ...     ...
73       Prof          B   18       10  Female  105450
74  AssocProf          B   19        6  Female  104542
75       Prof          B   17       17  Female  124312
76       Prof          A   28       14  Female  109954
77       Prof          A   23       15  Female  109646

[78 rows x 6 columns]

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

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