从 python 中的文本文件读取行(Windows)

发布于 2024-11-13 09:57:57 字数 853 浏览 3 评论 0原文

我正在开发一个简单的导入例程,将文本文件转换为 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 技术交流群。

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

发布评论

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

评论(4

那片花海 2024-11-20 09:57:57

Python 在枚举行时保留换行符。例如,在枚举文本文件时,

foo
bar

您会得到两个字符串:"foo\n""bar\n"。如果您不需要终端换行符,请调用 strip()

顺便说一句,我不喜欢这种行为。

Python keeps the new line characters while enumerating lines. For example, when enumerating a text file such as

foo
bar

you get two strings: "foo\n" and "bar\n". If you don't want the terminal new line characters, you call strip().

I am not a fan of this behavior by the way.

甚是思念 2024-11-20 09:57:57

请参阅

Python通常是用universal构建的
换行符支持;提供“U”打开
该文件作为文本文件,但行可能
因下列情况之一而终止:
Unix 行尾约定 '\n',
Macintosh 约定 '\r',或
Windows 约定 '\r\n'

See this.

Python is usually built with universal
newline support; supplying 'U' opens
the file as a text file, but lines may
be terminated by any of the following:
the Unix end-of-line convention '\n',
the Macintosh convention '\r', or the
Windows convention '\r\n'

青朷 2024-11-20 09:57:57

您需要 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.

过度放纵 2024-11-20 09:57:57

在 Python 解释器中尝试以下操作来查看该语言的功能:

open('test1.txt', 'wb').write(b'Hello\nWorld!')
open('test2.txt', 'wb').write(b'Hello\r\nWorld!')
print(list(open('test1.txt'))) # Shows ['Hello\n', 'World!']
print(list(open('test2.txt'))) # Shows ['Hello\n', 'World!']

Python 确实能够识别正确的换行符。您可能不想在字符串上使用 strip ,而是编写 myString.replace('\n', '') 。检查文档:

>>> help(str.strip)
Help on method_descriptor:

strip(...)
    S.strip([chars]) -> str

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.

>>> help(str.replace)
Help on method_descriptor:

replace(...)
    S.replace(old, new[, count]) -> str

    Return a copy of S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.

Try the following in a Python interpreter to see what the language does:

open('test1.txt', 'wb').write(b'Hello\nWorld!')
open('test2.txt', 'wb').write(b'Hello\r\nWorld!')
print(list(open('test1.txt'))) # Shows ['Hello\n', 'World!']
print(list(open('test2.txt'))) # Shows ['Hello\n', 'World!']

Python does recognize the correct newlines. Instead of using strip on your strings, you might want to write myString.replace('\n', '') instead. Check the documentation:

>>> help(str.strip)
Help on method_descriptor:

strip(...)
    S.strip([chars]) -> str

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.

>>> help(str.replace)
Help on method_descriptor:

replace(...)
    S.replace(old, new[, count]) -> str

    Return a copy of S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文