截至目前,我使用以下python代码:
file = open(filePath, "r")
lines=file.readlines()
file.close()
Run Code Online (Sandbox Code Playgroud)
假设我的文件有几行(10,000或更多),那么如果我为多个文件执行此操作,我的程序会变慢.有没有办法加快Python的速度?阅读各种链接我理解readlines将文件行存储在内存中,这就是代码变慢的原因.
我也尝试了以下代码,我得到的时间增加了17%.
lines=[line for line in open(filePath,"r")]
Run Code Online (Sandbox Code Playgroud)
python2.4中是否还有其他模块(我可能已经错过了).谢谢,桑迪亚
我有这样的数千个文件,我想提取对应于原子['CG','CD1','CD2','CE1','CE2','的行的列6,7,8的值. CZ'],
ATOM 1 CG TOLU 1 -0.437 -0.756 1.802 1.00 1.99 PRO0
ATOM 2 HG TOLU 1 -0.689 -1.123 2.786 1.00 0.00 PRO0
ATOM 3 CD1 TOLU 1 0.041 -1.623 0.811 1.00 1.99 PRO0
ATOM 4 HD1 TOLU 1 0.331 -2.603 1.162 1.00 0.00 PRO0
ATOM 5 CD2 TOLU 1 -0.692 0.547 1.352 1.00 1.99 PRO0
ATOM 6 HD2 TOLU 1 -1.131 1.264 2.030 1.00 0.00 PRO0
ATOM 7 CE1 TOLU 1 0.246 -1.276 -0.504 1.00 1.99 PRO0
ATOM …Run Code Online (Sandbox Code Playgroud) Python中的某些文件读取(readlines())函数
将文件内容复制到内存中(作为列表)
我需要处理一个太大而
无法复制到内存中的文件,因此需要使用
文件指针(一次一个字节地访问文件
) - 就像在C getc()中一样.
我的附加要求是
我想将文件指针倒回到前面的
字节,就像在C ungetc()中一样.
有没有办法在Python中执行此操作?
另外,在Python中,我可以
使用readline()一次 读取一行
有没有办法读取前一行
后退?
我目前正在寻找一种方法来编写Python中TXT文件的每一行的开头和结尾.例如,
目前的TXT文件:
Jimmy
Was
Here
Run Code Online (Sandbox Code Playgroud)
将第一个值写入每行的开头
111Jimmy
111Was
111Here
Run Code Online (Sandbox Code Playgroud)
将第二个值写入每行的末尾
111Jimmy222
111Was222
111Here222
Run Code Online (Sandbox Code Playgroud)
似乎无法在Google上找到任何描述如何正确完成此操作的内容.我已经找到了写入特定行的方法,但并不是所有这些都以这种方式写入.任何帮助表示赞赏!谢谢!
我正在做一个小程序来帮助我学习Python(我很新).我正在使用Python 3.2.
在Python shell中,当我进入
f = open('filename.txt', 'r')
f.readlines()
Run Code Online (Sandbox Code Playgroud)
它打印出来的一切filename.txt.但是,当我在新窗口中键入它并使用.py扩展名保存时,它在运行时不会显示任何输出.它也没有给我任何错误.
代码看起来有点像这样:
f = open('filename.txt', 'r')
f.readlines()
while True:
f = open('filename.txt', 'a')
inp = input('Enter text: ')
rest of code...
Run Code Online (Sandbox Code Playgroud)
在完成任何while声明之前,如何打印文件中的所有内容?
我有一个像这样说 3 行的 csv 文件:
Dallas
Houston
Ft. Worth
Run Code Online (Sandbox Code Playgroud)
我想要做的是能够读取它们并从中建立链接,但将所有行输出在一行上。示例输出需要是这样的:
<a href="/dallas/">Dallas</a> <a href="/houston/">Houston</a> <a href="/ft-worth/">Ft. Worth</a>
Run Code Online (Sandbox Code Playgroud)
这是我迄今为止的代码,它读取 csv 文件并输出,但它创建了不同的行,我只想要一行,另外我需要附加超链接的 html 代码。
f_in = open("data_files/states/major_cities.csv",'r')
for line in f_in.readlines():
f_out.write(line.split(",")[0]+"")
f_in.close()
f_out.close()
Run Code Online (Sandbox Code Playgroud) 我试图从txt文件添加行到python列表进行迭代,脚本想要打印每一行并返回错误.我正在使用readlines()函数,但是当我使用list.remove(lines)时,它会返回一个错误:File "quotes.py", line 20, in main list.remove(lines) TypeError: remove() takes exactly one argument (0 given).
def main():
while True:
try:
text_file = open("childrens-catechism.txt", "r")
lines = text_file.readlines()
# print lines
# print len(lines)
if len(lines) > 0:
print lines
list.remove(lines)
time.sleep(60)
else:
print "No more lines!"
break
text_file.close()
Run Code Online (Sandbox Code Playgroud)
我看不出我做错了什么.我知道它与list.remove()有关.先感谢您.
我有一个包含以下数据的文本文件:
5298 10036 4 360 8
6128 11947 2 385 7
9472 18930 0 233 4
5056 9790 1 293 6
Run Code Online (Sandbox Code Playgroud)
我使用以下代码读取此文件:
file1 = open("test.txt","r")
lines = file1.readlines()
BF=[map(float, line.split()) for line in lines]
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
could not convert string to float: ÿþ5
Run Code Online (Sandbox Code Playgroud)
为什么我看到这个错误?
更新:
print lines
Run Code Online (Sandbox Code Playgroud)
说明:
['\xff\xfe5\x002\x009\x008\x00\t\x001\x000\x000\x003\x006\x00\t\x004\x00\t\x003\x006\x000\x00\t\x008\x00\r\x00\n', '\x006\x001\x002\x008\x00\t\x001\x001\x009\x004\x007\x00\t\x002\x00\t\x003\x008\x005\x00\t\x007\x00\r\x00\n', '\x009\x004\x007\x002\x00\t\x001\x008\x009\x003\x000\x00\t\x000\x00\t\x002\x003\x003\x00\t\x004\x00\r\x00\n', '\x005\x000\x005\x006\x00\t\x009\x007\x009\x000\x00\t\x001\x00\t\x002\x009\x003\x00\t\x006\x00\r\x00\n', '\x001\x005\x000\x006\x004\x00\t\x003\x000\x001\x006\x000\x00\t\x001\x00\t\x003\x001\x002\x00\t\x008\x00']
Run Code Online (Sandbox Code Playgroud) 在Python中,假设我有:
f = open("file.txt", "r")
a = f.readlines()
b = f.readline()
print a
print b
Run Code Online (Sandbox Code Playgroud)
print a将显示文件的所有行并且print b不显示任何内容。
同样反之亦然:
f = open("file.txt", "r")
a = f.readline()
b = f.readlines()
print a
print b
Run Code Online (Sandbox Code Playgroud)
print a显示第一行,但print b将显示除第一行之外的所有行。
如果 和a都是breadlines(),a将显示所有行并且b不显示任何内容。
为什么会出现这种情况?为什么两个命令不能独立工作?有解决方法吗?
如何从python中的单词(docx)文件中读取。我可以读取 txt 文件,但不能读取 MS Office word 文档。有什么建议?
python ×10
readlines ×10
file ×2
readline ×2
text ×2
csv ×1
dictionary ×1
docx ×1
file-io ×1
list ×1
numpy ×1
python-3.x ×1
string ×1
text-files ×1
ungetc ×1