如何访问索引本身以获取如下列表?
ints = [8, 23, 45, 12, 78]
for i in ints:
print('item #{} = {}'.format(???, i))
Run Code Online (Sandbox Code Playgroud)
当我使用循环遍历它时for,如何访问循环索引,在这种情况下从1到5?
以下代码是否有其他替代方法:
startFromLine = 141978 # or whatever line I need to jump to
urlsfile = open(filename, "rb", 0)
linesCounter = 1
for line in urlsfile:
if linesCounter > startFromLine:
DoSomethingWithThisLine(line)
linesCounter += 1
Run Code Online (Sandbox Code Playgroud)
如果我正在(~15MB)使用未知但不同长度的行处理一个巨大的文本文件,并且需要跳转到我事先知道的特定行?当我知道我至少可以忽略文件的前半部分时,我会逐个处理它们.寻找更优雅的解决方案,如果有的话.
我有一个问题是在文件夹中解析1000个文本文件(每个文件大约3000行,大小约400KB).我确实用readlines读过它们,
for filename in os.listdir (input_dir) :
if filename.endswith(".gz"):
f = gzip.open(file, 'rb')
else:
f = open(file, 'rb')
file_content = f.readlines()
f.close()
len_file = len(file_content)
while i < len_file:
line = file_content[i].split(delimiter)
... my logic ...
i += 1
Run Code Online (Sandbox Code Playgroud)
这对我输入的样本(50,100个文件)完全没问题.当我在整个输入上运行超过5K的文件时,所花费的时间远不及线性增量.我计划进行性能分析并进行Cprofile分析.当输入达到7K文件时,更多文件以指数方式增加并且达到更差的速率所花费的时间.
这是readlines的累计时间,第一个 - > 354个文件(来自输入的样本)和第二个 - > 7473个文件(整个输入)
ncalls tottime percall cumtime percall filename:lineno(function)
354 0.192 0.001 **0.192** 0.001 {method 'readlines' of 'file' objects}
7473 1329.380 0.178 **1329.380** 0.178 {method 'readlines' of 'file' objects}
Run Code Online (Sandbox Code Playgroud)
因此,我的代码所花费的时间不会随着输入的增加而线性缩放.我阅读了一些文档说明readlines(),其中人们声称这readlines()会将整个文件内容读入内存,因此与readline()或相比通常消耗更多内存 …
当我尝试使用json.load()打开时,我有一个1.7 GB的JSON文件然后它给出了内存错误,如何读取python中的json文件?
我的JSON文件是包含特定键的大量对象.
编辑:如果它只是一个大的对象数组,并且事先知道对象的结构,那么就不需要使用我们可以逐行读取的工具.一行只包含数组的一个元素.我注意到这是json文件存储的方式,对我来说它只是工作
>>>for line in open('file.json','r').readline():
... do something with(line)
Run Code Online (Sandbox Code Playgroud) 我正在尝试解析一个大的 json 文件(数百个演出)以从其密钥中提取信息。为简单起见,请考虑以下示例:
import random, string
# To create a random key
def random_string(length):
return "".join(random.choice(string.lowercase) for i in range(length))
# Create the dicitonary
dummy = {random_string(10): random.sample(range(1, 1000), 10) for times in range(15)}
# Dump the dictionary into a json file
with open("dummy.json", "w") as fp:
json.dump(dummy, fp)
Run Code Online (Sandbox Code Playgroud)
然后,我在 python 2.7 中使用 ijson 来解析文件:
file_name = "dummy.json"
with open(file_name, "r") as fp:
for key in dummy.keys():
print "key: ", key
parser = ijson.items(fp, str(key) + ".item")
for number in …Run Code Online (Sandbox Code Playgroud) 我有数千个非常大的 JSON 文件,需要对特定元素进行处理。为了避免内存过载,我使用了一个名为ijson的 python 库,当我只处理 json 文件中的单个元素时,它工作得很好,但当我尝试一次处理多个元素时,它会通过
IncompleteJSONError:解析错误:过早的 EOF
部分 JSON:
{
"info": {
"added": 1631536344.112968,
"started": 1631537322.81162,
"duration": 14,
"ended": 1631537337.342377
},
"network": {
"domains": [
{
"ip": "231.90.255.25",
"domain": "dns.msfcsi.com"
},
{
"ip": "12.23.25.44",
"domain": "teo.microsoft.com"
},
{
"ip": "87.101.90.42",
"domain": "www.msf.com"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
工作代码:(打开多个文件)
my_file_list = [f for f in glob.glob("data/jsons/*.json")]
final_result = []
for filename in my_file_list:
row = {}
with open(filename, 'r') as f:
info = ijson.items(f, 'info')
for o …Run Code Online (Sandbox Code Playgroud) python ×6
json ×3
ijson ×2
list ×1
loops ×1
memory ×1
nltk ×1
performance ×1
python-2.6 ×1
python-2.7 ×1
readlines ×1
text-files ×1