Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
不带头的Python csv_Python_Csv - Fatal编程技术网

不带头的Python csv

不带头的Python csv,python,csv,Python,Csv,通过csv文件中的标题信息,城市可以被抓取为: city = row['city'] 现在,如何假设csv文件没有标题,只有一列,列是city。如果您自己声明标题,您仍然可以使用您的行,因为您知道它: with open('data.csv') as f: cf = csv.DictReader(f, fieldnames=['city']) for row in cf: print row['city'] 有关更多信息,请查看文档中的信息 另一种选择是只使用

通过csv文件中的标题信息,城市可以被抓取为:

city = row['city']

现在,如何假设csv文件没有标题,只有一列,列是city。

如果您自己声明标题,您仍然可以使用您的行,因为您知道它:

with open('data.csv') as f:
    cf = csv.DictReader(f, fieldnames=['city'])
    for row in cf:
        print row['city']
有关更多信息,请查看文档中的信息

另一种选择是只使用位置索引,因为您知道只有一列:

with open('data.csv') as f:
    cf = csv.reader(f)
    for row in cf:
        print row[0]

我正在使用熊猫数据帧对象:

df=pd.read_sql(sql_query,data_connection)
df.to_csv(filename, header=False, index=False)
我不知道这是否是最具python风格的方法,但它完成了任务。

您可以像@nosklo所描述的那样使用函数,如下所示:

df = pandas.read_csv("A2", header=None)
print df[0]


你看过csv文档了吗?您的代码正在读取sql?和写csv?你看过问题了吗?
df = pandas.read_csv("A2", header=None, names=(['city']))
print df['city']