python - 导入 csv - 过滤列 - 写入带有时间戳的 txt 文件 - txt 的输出问题
python - importing csv - filtering on column - writing to txt file w/ a timestamp - Output issues with txt
首先post,尽量不要对我的格式生气。
我正在尝试使用 python 3.5 在 csv 文件上进行 ETL - 我的代码成功提取,在正确的列上进行过滤,在 "new_string" 变量中创建所需的最终结果并生成运行 末尾正确命名的 txt 文件。但是打开 txt 文件显示它只有一个字符长如果它是一个索引 i = [1] 只是出现的东西,我期待整个列以字符串格式打印出来..显然我没有采用格式的 list/string 考虑在内,但我现在被卡住了。
如果有人看到这里发生了什么。我将不胜感激。提前致谢...
这是我的代码:
cdpath = os.getcwd()
def get_file_path(filename):
currentdirpath = os.getcwd()
file_path = os.path.join(os.getcwd(), filename)
print (file_path)
return file_path
path = get_file_path('cleanme.csv') ## My test file to work on
def timeStamped(fname, fmt='%Y-%m-%d-%H-%M-%S_{fname}'): ##Time stamp func
return datetime.datetime.now().strftime(fmt).format(fname=fname)
def read_csv(filepath):
with open(filepath, 'rU') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
new_list = row[2]
new_string = str(new_list)
print (new_string)
with open(timeStamped('cleaned.txt'),'w') as outf:
outf.write(new_string)
在您的代码中,您有:
def read_csv(filepath):
with open(filepath, 'rU') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
new_list = row[2]
new_string = str(new_list)
print (new_string)
with open(timeStamped('cleaned.txt'),'w') as outf:
outf.write(new_string)
正如我在上面的评论中指出的那样,有人质疑第二个 with 是否缩进正确,但实际上,这无关紧要:
您在 for 循环 (for row in reader
) 中生成了 new_string。但是因为你不在循环内使用它(除了打印出来),当循环结束时,你唯一可以访问的值将是最后一个元素。
或者,如果您将 with ... as outf
作为循环的一部分,则每次通过时,您都会打开一个新副本并覆盖数据,这样 cleaned.txt 就只有最后一个值又在最后。
我想你想要的是这样的:
def read_csv(filepath):
with open(filepath, 'rU') as csvfile:
with open(timeStamped('cleaned.txt'),'w') as outf:
reader = csv.reader(csvfile)
for row in reader:
new_list = row[2] #extract the 3rd column of each row
new_string = str(new_list) # optionally do some transforms here
print (new_string) #debug
outf.write(new_string) #store result
首先post,尽量不要对我的格式生气。
我正在尝试使用 python 3.5 在 csv 文件上进行 ETL - 我的代码成功提取,在正确的列上进行过滤,在 "new_string" 变量中创建所需的最终结果并生成运行 末尾正确命名的 txt 文件。但是打开 txt 文件显示它只有一个字符长如果它是一个索引 i = [1] 只是出现的东西,我期待整个列以字符串格式打印出来..显然我没有采用格式的 list/string 考虑在内,但我现在被卡住了。
如果有人看到这里发生了什么。我将不胜感激。提前致谢...
这是我的代码:
cdpath = os.getcwd()
def get_file_path(filename):
currentdirpath = os.getcwd()
file_path = os.path.join(os.getcwd(), filename)
print (file_path)
return file_path
path = get_file_path('cleanme.csv') ## My test file to work on
def timeStamped(fname, fmt='%Y-%m-%d-%H-%M-%S_{fname}'): ##Time stamp func
return datetime.datetime.now().strftime(fmt).format(fname=fname)
def read_csv(filepath):
with open(filepath, 'rU') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
new_list = row[2]
new_string = str(new_list)
print (new_string)
with open(timeStamped('cleaned.txt'),'w') as outf:
outf.write(new_string)
在您的代码中,您有:
def read_csv(filepath):
with open(filepath, 'rU') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
new_list = row[2]
new_string = str(new_list)
print (new_string)
with open(timeStamped('cleaned.txt'),'w') as outf:
outf.write(new_string)
正如我在上面的评论中指出的那样,有人质疑第二个 with 是否缩进正确,但实际上,这无关紧要:
您在 for 循环 (for row in reader
) 中生成了 new_string。但是因为你不在循环内使用它(除了打印出来),当循环结束时,你唯一可以访问的值将是最后一个元素。
或者,如果您将 with ... as outf
作为循环的一部分,则每次通过时,您都会打开一个新副本并覆盖数据,这样 cleaned.txt 就只有最后一个值又在最后。
我想你想要的是这样的:
def read_csv(filepath):
with open(filepath, 'rU') as csvfile:
with open(timeStamped('cleaned.txt'),'w') as outf:
reader = csv.reader(csvfile)
for row in reader:
new_list = row[2] #extract the 3rd column of each row
new_string = str(new_list) # optionally do some transforms here
print (new_string) #debug
outf.write(new_string) #store result