在每行 Python 之后添加的其他列表



在repl.it(工作正常,大概是因为Python中的错误已被修复/更新(和IDLE中的代码执行存在差异,其中代码无法正常工作。

我已经查阅了文档,以前的堆栈溢出答案以添加"换行符",但问题仍然存在。

你会注意到它,在这里:(完美工作(

https://repl.it/Jbv6/0

但是,在 IDLE 中粘贴文件内容(没有换行符(它工作正常

001,Joe,Bloggs,Test1:99,Test2:100,Test3:1002,Ash,Smith,Test1:20,Test2:20,Test3:100003003,Jonathan,Peter,Test1:99,Test2:33,Test3:44

但是将文件内容按原样粘贴到 TXT 文件中(每条记录都在新行上(时,如下所示:

001,Joe,Bloggs,Test1:99,Test2:100,Test3:1
002,Ash,Smith,Test1:20,Test2:20,Test3:100003
003,Jonathan,Peter,Test1:99,Test2:33,Test3:44

输出错误如下(每行后生成一个新列表(:

[['001', 'Joe', 'Bloggs', 'Test1:99', 'Test2:100', 'Test3:1'], [], ['002', 'Ash', 'Smith', 'Test1:20', 'Test2:20', 'Test3:100'], ['003'], ['', 'Jonathan', 'Peter', 'Test1:99', 'Test2:33', 'Test3:44']]

代码在这里:

import csv
#==========1. Open the File, Read it into a list, and Print Contents 
print("1==============Open File, Read into List, Print Contents")
#open the file, read it into a list (each line is a list within a list, and the end of line spaces are stripped as well as the individual elements split at the comma)
with open("studentinfo.txt","rb",newline="") as f:
studentlist=list(csv.reader(f))
print(studentlist)

我已经尝试过,正如文档和以前关于stackoverflow的答案所建议的那样,添加这个:(换行符(

with open("studentinfo.txt","r",newline="") as f:

不幸的是,错误仍然存在。

任何带有解释的建议/解决方案将不胜感激。

更新,我也试过这个:

with open("studentinfo.txt",newline="") as f:
reader=csv.reader(f)
for row in reader:
print(row)

同样,它在 replit 中完美运行

https://repl.it/Jbv6/2

但是这个错误在空闲

1==============Open File, Read into List, Print Contents
['001', 'Joe', 'Bloggs', 'Test1:99', 'Test2:100', 'Test3:1']
[]
['002', 'Ash', 'Smith', 'Test1:20', 'Test2:20', 'Test3:100']
['003']
['', 'Jonathan', 'Peter', 'Test1:99', 'Test2:33', 'Test3:44']
>>> 

对于需要能够在 repl.it 和 IDLE 之间保持一致的学生来说,这是一个巨大的问题,这就是他们在学校和家庭环境之间所做的工作。

任何显示允许它在两者上工作的代码的答案都是我所追求的。

最简单的答案如下:

import csv
# ==========1. Open the File, Read it into a list, and Print Contents 
print("1==============Open File, Read into List, Print Contents")
# open the file, read it into a list (each line is a list within a list,
# and the end of line spaces are stripped as well as the individual
# elements split at the comma)
studentlist = []
with open("studentinfo.txt", "r", newline="") as f:
for row in csv.reader(f):
if len(row) > 0:
studentlist.append(row)
print(studentlist)

但是你的原始代码应该可以工作 - 我已经运行了它,但在 linux 而不是 windows 上。如果我能要求你做更多的工作:

with open("studentinfo.txt", "r", newline="") as f:
ascii_ch = list(map(ord,f.read()))
eol_delims = list(map(str,(ch if ch < 32 else '' for ch in ascii_ch)))
print(",".join(eol_delims))

这将生成一个,列表,但穿插着13,1010,但甚至可能类似于10,13,10。这些是谈论的rnn,但我想知道您是否以某种方式设法获得了第三种选择? 如果是这样,我认为您需要重写该文本文件以获得正常的行尾。

--(更新以回应评论(
我对10,13,10的唯一建议是只在一个应用程序(例如记事本(中编辑文本文件,而不要在另一个应用程序中编辑它。

实际问题来自在两个应用程序中编辑文件,每个应用程序对行尾应该是什么有不同的解释(Windows应用程序应该是rn,"repl.it"是n。我以前遇到过它,但从未制定出所需的操作顺序。

尝试使用编解码器并显式指定文件的编码为 UTF-8。

import csv
import codecs
print("1==============Open File, Read into List, Print Contents")
with codecs.open("studentinfo.txt",encoding='utf-8') as f:
studentlist=list(csv.reader(f))
print(studentlist)

使用过滤器可能会有所帮助:

with open('studentinfo.txt', 'rU') as f:
filtered = (line.replace('r', '') for line in f)
for row in csv.reader(filtered):
print(row)

将字符串粘贴到文本编辑器中并保存文件不会在不同平台上生成字节相同的文件。(即使是同一平台上的不同编辑器也不一致!

但是,csv模块接受的 CSV 格式是根据字节精确表示形式指定的。可以使用方言(内置方言或实现新方言(自定义行为 - 有关详细信息,请参阅 Python 文档。默认方言是excel,这需要 Windows 样式的行尾 (CR/LF(。如果以其他格式保存文件,则无法正确解析该文件。

最新更新