Python:使用CSV解析变量,然后将其输出到另一个文件
我是一名服务器管理员。我不需要做太多脚本就顺利通过了,但是,唉,它已经抬起了它丑陋的头 小结:我有一个example.csv,如下所示Python:使用CSV解析变量,然后将其输出到另一个文件,python,Python,我是一名服务器管理员。我不需要做太多脚本就顺利通过了,但是,唉,它已经抬起了它丑陋的头 小结:我有一个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=d
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。在一行中循环。
将数据放入变量中。
输出最终产品打印?输入到输出文件中
循环直到EOF?
任何帮助都将不胜感激。像这样的东西应该有用 CSV模块对于像您这样的文件来说太过复杂了 我在这里使用的一些Python习惯用法: dictzipkeys,values-将键列表和值列表拉上拉链;dict函数或dict.update可以将它们作为键值对进行消化,以添加到dict中 映射表单字符串插值%foos然后可以消化dict 默认位在那里,因此字符串插值不会因缺少值而阻塞。适应你的需要
像这样的东西应该有用 CSV模块对于像您这样的文件来说太过复杂了 我在这里使用的一些Python习惯用法: dictzipkeys,values-将键列表和值列表拉上拉链;dict函数或dict.update可以将它们作为键值对进行消化,以添加到dict中 映射表单字符串插值%foos然后可以消化dict 默认位在那里,因此字符串插值不会因缺少值而阻塞。适应你的需要
你使我完整。不过,真的。非常感谢。所以,从我所知道的,你像我在阅读CSV的初学者代码中一样阅读CSV,但是你创建了一个字段?在本例中,这些字段基本上是第0-7行吗?当它读取您在for循环中输入的字段时,val命令更新这些字段并将其打印到模板中?@Ethabelle:添加了关于源代码每一位的适当注释:非常感谢:!你帮了大忙,解释得很好!试着用其他的方法来应用它,呵呵。这次是为了好玩!不客气!和往常一样,了解和解释这样一个脚本的最好方法是打印出它创建的值-print line.strip.split和print zipfields line.strip.split,在循环中有一个很好的尝试你使我完整。不过,真的。非常感谢。所以,从我所知道的,你像我在阅读CSV的初学者代码中一样阅读CSV,但是你创建了一个字段?在本例中,这些字段基本上是第0-7行吗?当它读取您在for循环中输入的字段时,val命令更新这些字段并将其打印到模板中?@Ethabelle:添加了关于源代码每一位的适当注释:非常感谢:!你帮了大忙,解释得很好!试着用其他的方法来应用它,呵呵。这次是为了好玩!不客气!和往常一样,了解和解释这样一个脚本的最好方法是打印出它创建的值-print line.strip.split和print zipfields line.strip.split,在循环中有一个很好的尝试
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