没有标题的Python csv

发布于 2024-09-12 10:10:40 字数 116 浏览 3 评论 0原文

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

city = row['city']

现在如何假设 csv 文件没有标题,只有 1 列,列是城市。

With header information in csv file, city can be grabbed as:

city = row['city']

Now how to assume that csv file does not have headers, there is only 1 column, and column is city.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

往昔成烟 2024-09-19 10:10:40

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

with open('data.csv') as f:
    cf = csv.DictReader(f, fieldnames=['city'])
    for row in cf:
        print row['city']

有关更多信息,请检查 csv.DictReader 文档中的信息。

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

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

You can still use your line, if you declare the headers yourself, since you know it:

with open('data.csv') as f:
    cf = csv.DictReader(f, fieldnames=['city'])
    for row in cf:
        print row['city']

For more information check csv.DictReader info in the docs.

Another option is to just use positional indexing, since you know there's only one column:

with open('data.csv') as f:
    cf = csv.reader(f)
    for row in cf:
        print row[0]
没企图 2024-09-19 10:10:40

您可以使用 pandas.read_csv() 函数,类似于@nosklo 描述的方式如下:

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

df = pandas.read_csv("A2", header=None, names=(['city']))
print df['city']

You can use pandas.read_csv() function similarly to the way @nosklo describes, as follows:

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

or

df = pandas.read_csv("A2", header=None, names=(['city']))
print df['city']
森罗 2024-09-19 10:10:40

对于没有标题的 CSV 文件,并且有超过 1 列,可以在 fieldnames=[]

背景:< 中按顺序添加其他列名称/strong> 我有一个由主机名组成的 CSV,后跟各自的 IP。

例如,

PLNXXXXA, 10.10.10.1
PLNXXXXB, 10.10.10.2

我想构建我的查找,键是 IP,相应的值是主机名以丰富我的数据。下面是代码:

import csv
csv_file =r"4.1.6_lookup.csv"

# Initialize an empty lookup dictionary
lookup = {}

# Read from the CSV file and populate the lookup dictionary
with open(csv_file, 'r') as f:
    csv_reader = csv.DictReader(f,fieldnames=['hostname','IP'])
    for row in csv_reader:
        # Capitalize the hostname and remove any leading/trailing whitespaces
        hostname = row['hostname'].upper().strip()  
        lookup[row['IP']] = hostname

输出如下:

{'10.10.10.1': 'PLNXXXXA', '10.10.10.2': 'PLNXXXXB'}

希望这对某人有帮助。
谢谢 ;)

For CSV file does not have headers, and there is more than 1 column, can add your other column names in order in the fieldnames=[]

Background: I have a CSV of consisitng of hostname, followed by respective IP.

e.g.

PLNXXXXA, 10.10.10.1
PLNXXXXB, 10.10.10.2

I want to build my lookup that key is IP, and respective value is hostname to enrich my data. Below are the code:

import csv
csv_file =r"4.1.6_lookup.csv"

# Initialize an empty lookup dictionary
lookup = {}

# Read from the CSV file and populate the lookup dictionary
with open(csv_file, 'r') as f:
    csv_reader = csv.DictReader(f,fieldnames=['hostname','IP'])
    for row in csv_reader:
        # Capitalize the hostname and remove any leading/trailing whitespaces
        hostname = row['hostname'].upper().strip()  
        lookup[row['IP']] = hostname

The output is going to be like:

{'10.10.10.1': 'PLNXXXXA', '10.10.10.2': 'PLNXXXXB'}

Hope this helps someone.
Thank you ;)

绝不服输 2024-09-19 10:10:40

我正在使用 pandas dataframe 对象:

df=pd.read_sql(sql_query,data_connection)
df.to_csv(filename, header=False, index=False)

不知道这是否是最 Pythonic 的方法,但它可以完成工作。

I'm using a pandas dataframe object:

df=pd.read_sql(sql_query,data_connection)
df.to_csv(filename, header=False, index=False)

Don't know if that is the most Pythonic approach, but it gets the job done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文