为什么输入函数不读取输入txt文件中的所有行?python
因此,我有一个项目,在这个项目中,我的程序读取一个txt文件,然后将txt转换为一个tab txt文件(基本上,它读取输入,使用DICIARY,当它找到一个特殊字符时,它会插入一个tab'\t')。该程序运行良好(到目前为止),但在找到换行符“\n”之前,它只读取第一行,我无法理解我的代码中发生了什么错误。有人能告诉我我的代码哪里出了问题吗 代码:为什么输入函数不读取输入txt文件中的所有行?python,python,input,Python,Input,因此,我有一个项目,在这个项目中,我的程序读取一个txt文件,然后将txt转换为一个tab txt文件(基本上,它读取输入,使用DICIARY,当它找到一个特殊字符时,它会插入一个tab'\t')。该程序运行良好(到目前为止),但在找到换行符“\n”之前,它只读取第一行,我无法理解我的代码中发生了什么错误。有人能告诉我我的代码哪里出了问题吗 代码: from Tkinter import * import Tkinter as tk import codecs from string impor
from Tkinter import *
import Tkinter as tk
import codecs
from string import *
u'\xe1'.encode('utf-8')
root = tk.Tk()
root.title('Tentative 1')
file = open('Data Path', 'r+')
#sentence = file.read()
#sentence = sentence.decode('cp1252', 'strict')
with codecs.open('Data path', encoding='latin1') as f:
sentence = f.readline()
if u'\xe1' in sentence:
print sentence
else:
pass
#sentence = sentence.replace("u'\xe1'", "-")
def task():
print '\n', sentence
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
reps = {'^^':'\t', '(':'\t', ')':'\t', 'ISBN:':'\t', '--':'\t', '"':'\t', '.:':'\t', '|':'\t', 'p.':'\t', ',':' '}
txt = replace_all(sentence, reps)
def txt_conversor():
txt = replace_all(sentence, reps)
print '\n', txt
results = tk.Button(root, text='Results', width=25, command=task)
results.pack()
txt = tk.Button(root, text='Convert results', width=25, command=txt_conversor)
txt.pack()
root.mainloop()
我尝试将f.readline()更改为f.readlines(),但出现错误:
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 323, in RunScript
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
File "C:\Python27\Lib\site-packages\Pythonwin\pywin\debugger\__init__.py", line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File "C:\Python27\Lib\site-packages\Pythonwin\pywin\debugger\debugger.py", line 654, in run
exec cmd in globals, locals
File "C:\Users\Joao\Desktop\Script (Console Bug with conversor).py", line 6, in <module>
import sys
File "C:\Users\Joao\Desktop\Script (Console Bug with conversor).py", line 233, in replace_all
text = text.replace(i, j)
AttributeError: 'list' object has no attribute 'replace'
输出:
Correia Teresa Pinto; Henriques Virgínia; Julião Rui Pedro 2013 IX Congresso da Geografia Portuguesa Geografia: Espaço Natureza Sociedade e Ciência 978-972-99436-6-9 Lisboa: Associação Portuguesa de Geógrafos. 977 e-Book
使用f.read()
f.readline()
只返回文件的下一行(包括换行符),因此它在处理第一行后会正确停止
f.readlines()
返回字符串列表,其中每个字符串对应于文件中的一行。本例中的问题是,您正在列表对象(语句
)上使用字符串方法
要修复此问题,您可以使用read()
,它以字符串形式返回文件的全部内容(这可能是最流行的解决方案),也可以确保将列表传递给replace\u all
函数(逐个处理列表项,使用.join()
等)
在这里,您可以找到不同文件方法的详细说明:读取文件的最佳方法
with open(filename, "r") as file:
for i in file.readlines():
print(i)
# end with (closes the file)
语句=f.readline()
专门读取一行<代码>读取行
正确。但是,您不能对整个列表进行替换
,您需要将其分别应用于列表中的每个项目。OP特别提到使用了读取行
,但也存在问题。@furas方法f.read()确实读取了所有信息,而且效果很好,谢谢@santoznma,这样你就可以将其中一个答案标记为已接受,如果你有足够的声誉,你就可以胜出。@furas!有一件事,为什么我的问题被否决了?因为它太简单了。答案对我们来说太明显了。我们有很多简单的问题-看起来人们不阅读文档和教程,不在SO或互联网上搜索现有答案,不尝试自己寻找解决方案,只在SO上等待答案。感谢@Railside的解释和回复!f.read()实际上对我有用!
sentence = f.read()
with open(filename, "r") as file:
for i in file.readlines():
print(i)
# end with (closes the file)