在 python 和 csv 模块中解析和保存 csv 文件
parsing and saving csv file in python and csv module
此脚本用于从 csv 文件解析和排序列表,并保存到新创建的 csv 文件,包括 headers。
我尝试包含写入函数,以将此解析器的输出保存到新的 csv 文件中,如下所示。此代码创建一个 csv,但仅在一列中记录 headers。
这是输入:
Timestamp,Session Index,Event,Description,Version,Platform,Device,User ID,Params,
"Dec 27, 2014 05:26 AM",1,NoRegister,,1.4.0,iPhone,Apple iPhone 5c (GSM),,{},
"Dec 27, 2014 05:24 AM",1,NoRegister,,1.4.0,iPhone,Apple iPhone 5c (GSM),,{},
"Dec 27, 2014 05:23 AM",1,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),,{ UserID : 54807; tabName : Home},
"Dec 27, 2014 05:23 AM",2,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),,{ UserID : 54807; tabName : Home},
"Dec 27, 2014 05:23 AM",3,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),,{ UserID : 54807; tabName : QuickAndEasy},
这是我想要保存到 csv 的输出:
Timestamp,Session Index,Event,Description,Version,Platform,Device,User ID,TabName,RecipeID,Type,SearchWord,IsFromLabel,
"Dec 27, 2014 05:26 AM",1,NoRegister,,1.4.0,iPhone,Apple iPhone 5c (GSM),,,,,,,
"Dec 27, 2014 05:24 AM",1,NoRegister,,1.4.0,iPhone,Apple iPhone 5c (GSM),,,,,,,
"Dec 27, 2014 05:23 AM",1,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),54807,Home,,,,,
"Dec 27, 2014 05:23 AM",2,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),54807,Home,,,,,
"Dec 27, 2014 05:23 AM",3,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),54807,QuickAndEasy,,,,,
代码:
import csv
def printfields(keys, linesets):
output_line = ""
for key in keys:
if key in linesets:
output_line += linesets[key] + ","
else:
output_line += ","
print output_line
def csvwriter(reader, path):
"""
write reader to a csv file path
"""
with open(path, "w") as csv_file:
writer = csv.writer(csv_file, delimiter=",")
for line1 in line:
if line1 in path:
writer.writerow(line1)
if __name__ == "__main__":
fields = [
"UserID", "tabName", "RecipeID", "type", "searchWord", "isFromLabel", "targetUID"
]
mappedLines = {}
with open('test.csv', 'r') as f:
reader = csv.DictReader(f)
for line in reader:
fieldPairs = [
p for p in
line['Params'].strip().strip('}').strip('{').strip().split(';')
if p
]
lineDict = {
pair.split()[0].strip(): pair.split(':')[1].strip()
for pair in fieldPairs
}
mappedLines[reader.line_num] = lineDict
path = "output.csv"
csvwriter(reader, path)
for key in sorted(mappedLines.keys()):
linesets = mappedLines[key]
printfields(fields, linesets)
csv_writer
引用符号 line
-- 这不是函数的参数。您希望如何提供?
你的代码有几个问题,
首先将其置于文件顶部
fields = [
"Timestamp","Session Index","Event","Description","Version","Platform","Device","User ID","Params",""
]
用DictWriter
写成dict
.
def csv_writer(lines, path):
"""
write reader to a csv file path
"""
with open(path, "w") as csv_file:
writer = csv.DictWriter(csv_file, fields)
writer.writeheader()
# Iterate over dict
for line1, val in lines.iteritems():
writer.writerow(val)
使用你的映射
csv_writer(mappedLines, path)
此脚本用于从 csv 文件解析和排序列表,并保存到新创建的 csv 文件,包括 headers。
我尝试包含写入函数,以将此解析器的输出保存到新的 csv 文件中,如下所示。此代码创建一个 csv,但仅在一列中记录 headers。
这是输入:
Timestamp,Session Index,Event,Description,Version,Platform,Device,User ID,Params,
"Dec 27, 2014 05:26 AM",1,NoRegister,,1.4.0,iPhone,Apple iPhone 5c (GSM),,{},
"Dec 27, 2014 05:24 AM",1,NoRegister,,1.4.0,iPhone,Apple iPhone 5c (GSM),,{},
"Dec 27, 2014 05:23 AM",1,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),,{ UserID : 54807; tabName : Home},
"Dec 27, 2014 05:23 AM",2,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),,{ UserID : 54807; tabName : Home},
"Dec 27, 2014 05:23 AM",3,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),,{ UserID : 54807; tabName : QuickAndEasy},
这是我想要保存到 csv 的输出:
Timestamp,Session Index,Event,Description,Version,Platform,Device,User ID,TabName,RecipeID,Type,SearchWord,IsFromLabel,
"Dec 27, 2014 05:26 AM",1,NoRegister,,1.4.0,iPhone,Apple iPhone 5c (GSM),,,,,,,
"Dec 27, 2014 05:24 AM",1,NoRegister,,1.4.0,iPhone,Apple iPhone 5c (GSM),,,,,,,
"Dec 27, 2014 05:23 AM",1,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),54807,Home,,,,,
"Dec 27, 2014 05:23 AM",2,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),54807,Home,,,,,
"Dec 27, 2014 05:23 AM",3,HomeTab,Which tab the user viewed ,1.4.0,iPhone,Apple iPhone 5s (GSM),54807,QuickAndEasy,,,,,
代码:
import csv
def printfields(keys, linesets):
output_line = ""
for key in keys:
if key in linesets:
output_line += linesets[key] + ","
else:
output_line += ","
print output_line
def csvwriter(reader, path):
"""
write reader to a csv file path
"""
with open(path, "w") as csv_file:
writer = csv.writer(csv_file, delimiter=",")
for line1 in line:
if line1 in path:
writer.writerow(line1)
if __name__ == "__main__":
fields = [
"UserID", "tabName", "RecipeID", "type", "searchWord", "isFromLabel", "targetUID"
]
mappedLines = {}
with open('test.csv', 'r') as f:
reader = csv.DictReader(f)
for line in reader:
fieldPairs = [
p for p in
line['Params'].strip().strip('}').strip('{').strip().split(';')
if p
]
lineDict = {
pair.split()[0].strip(): pair.split(':')[1].strip()
for pair in fieldPairs
}
mappedLines[reader.line_num] = lineDict
path = "output.csv"
csvwriter(reader, path)
for key in sorted(mappedLines.keys()):
linesets = mappedLines[key]
printfields(fields, linesets)
csv_writer
引用符号 line
-- 这不是函数的参数。您希望如何提供?
你的代码有几个问题,
首先将其置于文件顶部
fields = [
"Timestamp","Session Index","Event","Description","Version","Platform","Device","User ID","Params",""
]
用DictWriter
写成dict
.
def csv_writer(lines, path):
"""
write reader to a csv file path
"""
with open(path, "w") as csv_file:
writer = csv.DictWriter(csv_file, fields)
writer.writeheader()
# Iterate over dict
for line1, val in lines.iteritems():
writer.writerow(val)
使用你的映射
csv_writer(mappedLines, path)