gpt4 book ai didi

Python:使用 CSV 解析(?)变量,然后将其输出到另一个文件

转载 作者:行者123 更新时间:2023-11-28 22:54:12 25 4
gpt4 key购买 nike

我是服务器管理员。我在没有编写太多脚本的情况下就过去了,但是唉——它已经抬起了丑陋的头。

总结:我有 example.csv,如下所示;

Stan,Marsh,Stan Marsh,1001,899,smarsh,smarsh@info.com
Eric,Cartman,Eric Cartman,1002,898,ecartman,ecartman@info.com

现在。我正在尝试读取 csv 文件。然后,我想从每一行中取出值并将其放入这样的东西中;

dn: cn=$CN,ou=People,dc=domain,dc=com
cn: $CN
gidnumber: 20
givenname $FN
homedirectory /home/users/$USER
loginshell: /bin/sh
objectclass: inetOrgPerson
objectclass: posixAccount
objectclass: top
sn: $LN
uid: $USERNAME
telephoneNumber: $TELE
uidnumber: $UIDN
userpassword: {CRYPT}mrpoo
mail: $EMAIL

如您所见,我正在尝试制作一个允许我导入用户名然后自动填充变量的 LDIF 文件。

我似乎无法将各个部分拼凑起来。

我也没有走多远。我学会了打印行,耶...!

import csv

with open('example.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
print row

我认为逻辑如下。

  • 导入 .CSV。遍历行。
  • 将数据放入变量。
  • 将最终产品(打印件?)输出到“Output_File”
  • 循环直到 EOF?

如有任何帮助,我们将不胜感激。

最佳答案

像这样的东西应该行得通。

CSV 模块对于像您这样的文件来说太过分了。

我在这里使用的一些 Python 习语:

  • dict(zip(keys, values)) -- 压缩键列表和值列表; dict 函数(或 dict.update)可以将它们消化为键值对以添加到字典中
  • 映射形式的字符串插值 (%(foo)s) 然后可以消化一个 dict

defaults 位在那里,因此字符串插值不会因缺失值而阻塞。适应您的需求。 :)

.

if True:  # For testing -- use the other branch to read from a file
# Declare some test content in a string...
input_content = """
Stan,Marsh,Stan Marsh,1001,899,smarsh,smarsh@info.com
Eric,Cartman,Eric Cartman,1002,898,ecartman,ecartman@info.com
""".strip()
# And use the StringIO module to create a file-like object from it.
from StringIO import StringIO
input_file = StringIO(input_content)
else:
# Or just open the file as normal. In a short script like this,
# one doesn't need to worry about closing the file - that will happen
# when the script ends.
input_file = open('example.csv', 'rb')


# Declare the fields in the order they are in the file.
# zip() will use this later with the actual fields from the file
# to create a dict mapping.
fields = ('FN', 'LN', 'NAME', 'UIDN', 'GIDN', 'CN', 'EMAIL') # Fields, in order

# Declare a template for the LDIF file. The %(...)s bits will be
# later interpolated with the dict mapping created for each input row.
template = u"""
dn: cn=%(CN)s,ou=People,dc=domain,dc=com
cn: %(CN)s
gidnumber: 20
givenname %(FN)s
homedirectory /home/users/%(USER)s
loginshell: /bin/sh
objectclass: inetOrgPerson
objectclass: posixAccount
objectclass: top
sn: %(LN)s
uid: %(USERNAME)s
telephoneNumber: %(TELE)s
uidnumber: %(UIDN)s
userpassword: {CRYPT}mrpoo
mail: %(EMAIL)s
"""

for line in input_file:
# Create `vals` with some default values. These would be overwritten
# if the CSV data (and of course the declared fields) contain them.
vals = {"USER": "XXX", "TELE": "XXX", "USERNAME": "XXX"}

# line.strip().split() will turn the string line,
# for example 'foo,baz,bar\n' (trailing new line `strip`ped out)
# into the list ['foo', 'baz', 'bar'].
# zipping it with, say, ['LN', 'FN', 'EMAIL'] would yield
# [('LN', 'foo'), ('FN', 'baz'), ('EMAIL', 'bar')] --
# ie. a list of tuples with a key and a value.
# This can be used by the `dict.update` function to replace and augment
# the default values declared above.

vals.update(zip(fields, line.strip().split(",")))

# Finally, use the interpolation operator % to merge the template with the
# values for this line and print it to standard output.

print template % vals

关于Python:使用 CSV 解析(?)变量,然后将其输出到另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18327249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com