Python 帮助读取 csv 文件由于行结束而失败
我正在尝试创建此脚本,该脚本将检查计算机主机名,然后在主列表中搜索该值以返回 csv 文件中的相应值。然后打开另一个文件并进行查找替换。我知道这应该很容易,但以前没有在 python 中做过这么多。这是我到目前为止所拥有的...
masterlist.txt (tab delimited)
Name UID
Bob-Smith.local bobs
Carmen-Jackson.local carmenj
David-Kathman.local davidk
Jenn-Roberts.local jennr
这是我到目前为止创建的脚本
#GET CLIENT HOST NAME
import socket
host = socket.gethostname()
print host
#IMPORT MASTER DATA
import csv, sys
filename = "masterlist.txt"
reader = csv.reader(open(filename, "rU"))
#PRINT MASTER DATA
for row in reader:
print row
#SEARCH ON HOSTNAME AND RETURN UID
#REPLACE VALUE IN FILE WITH UID
#import fileinput
#for line in fileinput.FileInput("filetoreplace",inplace=1):
# line = line.replace("replacethistext","UID")
# print line
现在,它只是设置为打印主列表。我不确定是否需要解析该列表并将其放入字典或其他什么中。我真的需要弄清楚如何在第一个字段中搜索主机名,然后返回第二列中的字段。
预先感谢您的帮助, Aaron
更新:我从 masterlist.txt 中删除了第 194 行和最后一行,然后重新运行该脚本。结果如下:
回溯(最近一次调用最后一次):
文件“update.py”,第 3 行,位于 对于 csv.DictReader 中的行(open(fname), 分隔符='\t'): 文件 “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/csv.py”, 第 103 行,下一个 self.fieldnames 文件“/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/csv.py”, 第 90 行,在字段名中 self._fieldnames = self.reader.next() _csv.Error:在未加引号的字段中看到换行符 - 是否需要打开 文件处于通用换行模式吗?
当前使用的脚本是...
import csv
fname = "masterlist.txt"
for row in csv.DictReader(open(fname), delimiter='\t'):
print(row)
I'm trying to create this script that will check the computer host name then search a master list for the value to return a corresponding value in the csv file. Then open another file and do a find an replace. I know this should be easy but haven't done so much in python before. Here is what I have so far...
masterlist.txt (tab delimited)
Name UID
Bob-Smith.local bobs
Carmen-Jackson.local carmenj
David-Kathman.local davidk
Jenn-Roberts.local jennr
Here is the script that I have created thus far
#GET CLIENT HOST NAME
import socket
host = socket.gethostname()
print host
#IMPORT MASTER DATA
import csv, sys
filename = "masterlist.txt"
reader = csv.reader(open(filename, "rU"))
#PRINT MASTER DATA
for row in reader:
print row
#SEARCH ON HOSTNAME AND RETURN UID
#REPLACE VALUE IN FILE WITH UID
#import fileinput
#for line in fileinput.FileInput("filetoreplace",inplace=1):
# line = line.replace("replacethistext","UID")
# print line
Right now, it's just set to print the master list. I'm not sure if the list needs to be parsed and placed into a dictionary or what. I really need to figure out how to search the first field for the hostname and then return the field in the second column.
Thanks in advance for your help,
Aaron
UPDATE: I removed line 194 and last line from masterlist.txt and then re-ran the script. The results were the following:
Traceback (most recent call last):
File "update.py", line 3, in
for row in csv.DictReader(open(fname),
delimiter='\t'): File
"/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/csv.py",
line 103, in next
self.fieldnames File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/csv.py",
line 90, in fieldnames
self._fieldnames = self.reader.next()
_csv.Error: new-line character seen in unquoted field - do you need to open
the file in universal-newline mode?
The current script being used is...
import csv
fname = "masterlist.txt"
for row in csv.DictReader(open(fname), delimiter='\t'):
print(row)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第 194 行和最后一行中两次出现 '\xD5' 与该问题无关。
该问题似乎是 Python 2.6 csv 模块中的错误、误导性错误消息或不正确/模糊的文档。
在文件中,行以“\x0D”(在经典 Mac 传统中又名“\r”)结束。最后一行没有终止,但这与问题无关。
csv.reader 文档 说“如果 csvfile 是文件对象,它必须在有影响的平台上使用“b”标志打开。”众所周知,它在 Windows 上确实有所不同。然而,在这种情况下,使用“rb”或“r”打开文件没有什么区别——仍然是相同的错误消息。
csv.Dialect.lineterminator 的文档说“使用的字符串终止编写器生成的行。它默认为“\r\n”。注意:读取器被硬编码为将“\r”或“\n”识别为行结束符,并忽略行终止符。未来的行为可能会改变。”它似乎将 '\r' 识别为换行符,但不识别为行尾/字段尾。
错误消息“_csv.Error:在未加引号的字段中看到换行符 - 是否需要以通用换行模式打开文件?”令人困惑;它被识别为换行符,但它不会将换行符视为行尾(因此隐式地视为字段结束)。
似乎有必要以“rU”模式打开文件才能使其“工作”。目前尚不清楚为什么在通用换行模式下识别的相同“\r”会更好。
The two occurrences of '\xD5' in line 194 and the last line have nothing to do with the problem.
The problem appears to be a bug, or a misleading error message, or incorrect/vague documentation, in the Python 2.6 csv module.
In the file, the lines are terminated by '\x0D' aka '\r' in the Classic Mac tradition. The last line is not terminated, but that is nothing to do with the problem.
The docs for csv.reader say "If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference." It is widely known that it does make a difference on Windows. However opening the file with 'rb' or 'r' makes no difference in this case -- still the same error message.
The docs for csv.Dialect.lineterminator say "The string used to terminate lines produced by the writer. It defaults to '\r\n'. Note: The reader is hard-coded to recognise either '\r' or '\n' as end-of-line, and ignores lineterminator. This behavior may change in the future." It appears to be recognising '\r' as new-line but not as end-of-line/end-of-field.
The error message "_csv.Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?" is confusing; it's recognised '\r' as a new-line, but it's not treating new-line as an end-of line (and thus implicitly end-of-field).
It appears necessary to open the file in 'rU' mode to get it to "work". It's not apparent why the same '\r' recognised in universal-newline mode is any better.
要迭代阅读器,您可以这样做:
但是由于您希望将
Name
与UID
关联起来:To get iterate over a reader you'd do:
But since you want to associate
Name
withUID
:我会像这样填充字典:
I would populate a dictionary like this: