나는 각각의 json 파일을 약 1.5gb로하여 하나의 csv (R로로드)로 결합하여 출력 할 json 파일을 다수 보유하고 있습니다. 각 250mb에서 4-5 개의 json 파일에 대한 시험판을 작성하는 동안 다음 오류가 발생합니다. 나는 8GB RAM과 Windows 7 Professional 64 비트에서 Python 버전 '2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]'
을 실행 중입니다.Python의 MemoryError : 코드를 최적화하려면 어떻게해야합니까?
저는 파이썬 초보자이며 최적화 된 코드를 작성해 본 경험이 거의 없으며 아래에서 스크립트를 최적화 할 수있는 방법에 대한 지침을 알려 주시면 감사하겠습니다. 고맙습니다!
======= 파이썬 MemoryError의의 =======
Traceback (most recent call last):
File "C:\Users\...\tweetjson_to_csv.py", line 52, in <module>
for line in file:
MemoryError
[Finished in 29.5s]
======= JSON CSV로 변환 스크립트 =======
# csv file that you want to save to
out = open("output.csv", "ab")
filenames = ["8may.json", "9may.json", "10may.json", "11may.json", "12may.json"]
open_files = map(open, filenames)
# change argument to the file you want to open
for file in open_files:
for line in file:
# only keep tweets and not the empty lines
if line.rstrip():
try:
tweets.append(json.loads(line))
except:
pass
for tweet in tweets:
ids.append(tweet["id_str"])
texts.append(tweet["text"])
time_created.append(tweet["created_at"])
retweet_counts.append(tweet["retweet_count"])
... ...
print >> out, "ids,text,time_created,retweet_counts,in_reply_to,geos,coordinates,places,country,language,screen_name,followers,friends,statuses,locations"
rows = zip(ids,texts,time_created,retweet_counts,in_reply_to_screen_name,geos,coordinates,places,places_country,lang,user_screen_names,user_followers_count,user_friends_count,user_statuses_count,user_locations)
csv = writer(out)
for row in rows:
values = [(value.encode('utf8') if hasattr(value, 'encode') else value) for value in row]
csv.writerow(values)
out.close()
여기
당신은 메모리에 모든 것을 ('tweets.append (json.loads (선))')로드하고 있습니다. 각 라인을 읽은 직후'output.csv'에 쓰는 것과 같은 방식으로 알고리즘을 구사할 수 있습니까? – univerio
이것은 아마도 http://codereview.stackexchange.com에 더 적합 할 것입니다. – dano
그러나 여기에있는 동안 한 번에 하나씩 파일을 열어야합니다. 한꺼번에 열 필요가 없습니다. 특히 당신이 그들과 함께 끝났을 때 당신이 그들을 닫지 않을 것이기 때문에. 덕분에 @ dano. – dano