从 python 中的文本文件读取行(Windows)
我正在开发一个简单的导入例程,将文本文件转换为 python 系统的 json 文件格式。
import json # Open text file for reading txtFile = open('Boating.Make.txt', 'r') # Create picklist obj picklistObj = dict() picklistObj['name'] = 'Boating.Make' picklistObj['items'] = list() i = 0 # Iterate through each make in text file for line in txtFile: picklistItemObj = dict() picklistItemObj['value'] = str(i) picklistItemObj['text'] = line.strip() picklistItemObj['selectable'] = True picklistObj['items'].append(picklistItemObj) i = i + 1 txtFile.close() picklistJson = json.dumps(picklistObj, indent=4) print picklistJson picklistFile = open('Boating.Make.json', 'w') picklistFile.write(picklistJson) picklistFile.close()
我的问题是,为什么我需要“条”?我认为 python 应该神奇地知道我当前所处的任何环境的换行符常量。我错过了什么吗?
我应该澄清一下,我正在读取的文本文件是一个 ASCII 文件,其中包含以“\r\n”分隔的文本行。
I am working on a simple import routine that translates a text file to a json file format for our system in python.
import json # Open text file for reading txtFile = open('Boating.Make.txt', 'r') # Create picklist obj picklistObj = dict() picklistObj['name'] = 'Boating.Make' picklistObj['items'] = list() i = 0 # Iterate through each make in text file for line in txtFile: picklistItemObj = dict() picklistItemObj['value'] = str(i) picklistItemObj['text'] = line.strip() picklistItemObj['selectable'] = True picklistObj['items'].append(picklistItemObj) i = i + 1 txtFile.close() picklistJson = json.dumps(picklistObj, indent=4) print picklistJson picklistFile = open('Boating.Make.json', 'w') picklistFile.write(picklistJson) picklistFile.close()
My question is, why do I need the "strip"? I thought that python was supposed to magically know the newline constant for whatever environment I am currently in. Am I missing something?
I should clarify that the text file I am reading from is an ASCII file that contains lines of text separated '\r\n'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Python 在枚举行时保留换行符。例如,在枚举文本文件时,
您会得到两个字符串:
"foo\n"
和"bar\n"
。如果您不需要终端换行符,请调用strip()
。顺便说一句,我不喜欢这种行为。
Python keeps the new line characters while enumerating lines. For example, when enumerating a text file such as
you get two strings:
"foo\n"
and"bar\n"
. If you don't want the terminal new line characters, you callstrip()
.I am not a fan of this behavior by the way.
请参阅此。
See this.
您需要 strip() 因为“for line in file:”将行终止符保留在行上。文档中没有明确说明(至少在我正在查看的 2.71 文档中)。但它的功能与 file.readline() 类似,它明确声明它保留换行符。
You need the strip() because "for line in file:" keeps the line terminators on the lines. It's not explicitly stated in the docs (at least in the 2.71 doc I'm looking at). But it functions in a fashion similar to file.readline(), which does explicitly state that it retains the newline.
在 Python 解释器中尝试以下操作来查看该语言的功能:
Python 确实能够识别正确的换行符。您可能不想在字符串上使用
strip
,而是编写myString.replace('\n', '')
。检查文档:Try the following in a Python interpreter to see what the language does:
Python does recognize the correct newlines. Instead of using
strip
on your strings, you might want to writemyString.replace('\n', '')
instead. Check the documentation: