【发布时间】:2018-03-22 17:17:35
【问题描述】:
我遇到的问题,似乎没有任何答案,是我需要处理一个非常大的文本文件(来自 GUDID 的 gmdnTerms.txt 文件),操作数据以合并行重复 ID,为键值对创建适当的列,并将结果转储到 CSV 文件。除了实现多线程之外,我已经尽我所能来提高效率。我需要能够对迭代文本文件和构建数据框的过程进行多线程处理。多线程教程没有太大帮助。希望有经验的Python程序员能给出明确的答案。下面是整个程序。请帮忙,在 4.7GHz proc(8 核)、16GB RAM 和 SSD 上,当前运行时间超过 20 小时。
#Assumptions this program makes:
#That duplicate product IDs will immediately follow each other
#That the first line of the text file contains only the keys and no values
#That the data lines are delimited by a "\n" character
#That the individual values are delimited by a "|" character
#The first value in each line will always be a unique product ID
#Each line will have exactly 3 values
#Each line's values will always be in the same order
#Import necessary libraries
import os
import pandas as pd
import mmap
import time
#Time to run
startTime = time.time()
#Parameters of the program
fileLocation = "C:\\Users\User\....\GMDNTest.txt"
outCSVFile = "GMDNTermsProcessed.csv"
encodingCSVFile = "utf-8"
#Sets up variables to be used later on
df = pd.DataFrame()
keys = []
idx = 0
keyNum = 0
firstLine = True
firstValue = True
currentKey = ''
#This loops over each line in text file and collapses lines with duplicate Product IDs while building new columns for appropriate keys and values
#These collapsed lines and new columns are stored in a dataframe
with open (fileLocation, "r+b") as myFile:
map = mmap.mmap(myFile.fileno(), 0, access=mmap.ACCESS_READ)
for line in iter(map.readline, ""):
#Gets keys from first line, splits them, stores in list
if firstLine == True:
keyRaw = line.split("|")
keyRaw = [x.strip() for x in keyRaw]
keyOne = keyRaw[0]
firstLine = False
#All lines after first go through this
#Collapses lines by comparing the unique ID
#Stores collapsed KVPs into a dataframe
else:
#Appends which number of key we are at to the key and breaks up the values into a list
keys = [x + "_" + str(keyNum) for x in keyRaw]
temp = line.split("|")
temp = [x.strip() for x in temp]
#If the key is the same as the key on the last line this area is run through
#If this is the first values line it also goes through here
if temp[0] == currentKey or firstValue == True:
#Only first values line hits this part; gets first keys and builds first new columns
if firstValue == True:
currentKey = temp[0]
df[keyOne] = ""
df.at[idx, keyOne] = temp[0]
df[keys[1]] = ""
df.at[idx, keys[1]] = temp[1]
df[keys[2]] = ""
df.at[idx, keys[2]] = temp[2]
firstValue = False
#All other lines with the same key as the last line go through here
else:
headers = list(df.columns.values)
if keys[1] in headers:
df.at[idx, keys[1]] = temp[1]
df.at[idx, keys[2]] = temp[2]
else:
df[keys[1]] = ""
df.at[idx, keys[1]] = temp[1]
df[keys[2]] = ""
df.at[idx, keys[2]] = temp[2]
#If the current line has a different key than the last line this part is run through
#Sets new currentKey and adds values from that line to the dataframe
else:
idx+=1
keyNum = 0
currentKey = temp[0]
keys = [x + "_" + str(keyNum) for x in keyRaw]
df.at[idx, keyOne] = temp[0]
df.at[idx, keys[1]] = temp[1]
df.at[idx, keys[2]] = temp[2]
#Don't forget to increment that keyNum
keyNum+=1
#Dumps dataframe of collapsed values to a new CSV file
df.to_csv(outCSVFile, encoding=encodingCSVFile, index=False)
#Show us the approx runtime
print("--- %s seconds ---" % (time.time() - startTime))
【问题讨论】:
-
您的输入文件有多大?
-
输入样本和输出样本也会有所帮助。
-
@Steve 这是测试输入数据pastebin.com/z8nKX22t,这是该数据的结果(以 CSV 格式打开)pastebin.com/dhmtKbGE
-
啊好的,输入文件有多大?我想知道它是否适合内存
-
@Steve 抱歉,忘记包含实际文件的大小。它的大小约为 880MB。
标签: python multithreading pandas bigdata text-analysis