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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6229910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:59:28  来源:igfitidea点击:

Reading lines from text file in python (windows)

pythonwindowsjsonio

提问by feathj

I am working on a simple import routine that translates a text file to a json file format for our system in 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()

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?

我的问题是,为什么我需要“条”?我认为 python 应该神奇地知道我目前所处的任何环境的换行符常量。我错过了什么吗?

I should clarify that the text file I am reading from is an ASCII file that contains lines of text separated '\r\n'.

我应该澄清一下,我正在读取的文本文件是一个 ASCII 文件,其中包含以 '\r\n' 分隔的文本行。

采纳答案by Eser Aygün

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

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

foo
bar

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

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

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

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

回答by Akash

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'

Python 通常内置通用换行支持;提供 'U' 将文件作为文本文件打开,但行可以由以下任何一种终止:Unix 行尾约定 '\n'、Macintosh 约定 '\r' 或 Windows 约定 '\ r\n'

回答by Noctis Skytower

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

在 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 does recognize the correct newlines. Instead of using stripon your strings, you might want to write myString.replace('\n', '')instead. Check the documentation:

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.

回答by John Gaines Jr.

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.

您需要 strip() 因为“for line in file:”将行终止符保留在行上。它没有在文档中明确说明(至少在我正在查看的 2.71 文档中)。但它的功能类似于 file.readline(),它明确声明它保留了换行符。