这对于调试很有用(因此编程相关).在linux上,我们可以使用该命令
strace -feopen python myfile.py
Run Code Online (Sandbox Code Playgroud)
找出加载了哪些python模块和共享对象.macOS X上有一个等效的单行程吗?
我有一个9GB的推文文本文件,格式如下:
T 'time and date'
U 'name of user in the form of a URL'
W Actual tweet
Run Code Online (Sandbox Code Playgroud)
总共有6,000,000个用户和超过60,000,000条推文.我使用itertools.izip()一次读取3行,然后根据名称将其写入文件.但它采取的方式太长(26小时和计数).怎么能更快?
发布完整性代码,
s='the existing folder which will have all the files'
with open('path to file') as f:
for line1,line2,line3 in itertools.izip_longest(*[f]*3):
if(line1!='\n' and line2!='\n' and line3!='\n'):
line1=line1.split('\t')
line2=line2.split('\t')
line3=line3.split('\t')
if(not(re.search(r'No Post Title',line1[1]))):
url=urlparse(line3[1].strip('\n')).path.strip('/')
if(url==''):
file=open(s+'junk','a')
file.write(line1[1])
file.close()
else:
file=open(s+url,'a')
file.write(line1[1])
file.close()
Run Code Online (Sandbox Code Playgroud)
我的目标是在小文本上使用主题建模(例如,在一个用户的所有推文上运行lda,因此每个用户需要一个单独的文件),但它花费了太多时间.
更新:我使用了S.Lott用户的建议并使用了以下代码:
import re
from urlparse import urlparse
import os
def getUser(result):
result=result.split('\n')
u,w=result[0],result[1]
path=urlparse(u).path.strip('/')
if(path==''):
f=open('path to junk','a')
f.write('its …Run Code Online (Sandbox Code Playgroud) 我正在编写一个python程序来逐行将文件复制到一个新文件中.我的代码如下所示,我使用循环逐行复制文件.
但是,由于文件中的行数可能会改变,有没有办法在python中逐行复制文件而不使用依赖于数字的循环,而是依赖于类似EOF字符的东西来终止循环?
import os
import sys
i = 0
f = open("C:\\Users\\jgr208\\Desktop\\research_12\\sap\\beam_springs.$2k","r")
copy = open("C:\\Users\\jgr208\\Desktop\\research_12\\sap\\copy.$2k","wt")
#loop that copies file line by line and terminates loop when i reaches 10
while i < 10:
line = f.readline()
copy.write(str(line))
i = i +1
f.close()
copy.close()
Run Code Online (Sandbox Code Playgroud)