python - After a desire ouput Not able to print date from both date/time
问题描述
Hello Experts i have a program that read the csv file which contain several columns main motive of this program is to convert the string into seuence of number and duplicated string will be the same number which have taken this all operation i can able to perform but I want my date/time column to print only date for that i applied a slicing method that's work in console but I'm not able to to print it on my other csv file. Please tell me what to do.
This is the program I have written:
import pandas as pd
import csv
import os
# from io import StringIO
# tempFile="input1.csv"
with open("input1.csv", 'r',encoding="utf8") as csvfile:
# creating a csv reader object
reader = csv.DictReader(csvfile, delimiter=',')
# next(reader, None)
'''We then restructure the data to be a set of keys with list of values {key_1: [], key_2: []}:'''
data = {}
for row in reader:
# print(row)
for header, value in row.items():
try:
data[header].append(value)
except KeyError:
data[header] = [value]
'''Next we want to give each value in each list a unique identifier.'''
# Loop through all keys
for key in data.keys():
values = data[key]
things = list(sorted(set(values), key=values.index))
for i, x in enumerate(data[key]):
if key=="Date/Time":
var = data[key]
iter_obj1 = iter(var)
while True:
try:
element1 = next(iter_obj1)
date =element1[0:10]
print("date-",date)
except StopIteration:
break
break
else:
# if key == "Date/Time" :
# print(x[0:10])
# continue
data[key][i] = things.index(x) + 1
print('data.[keys]()-',data[key])
print('data.keys()-',data.keys())
print('values-',values)
print('data.keys()-',key)
print('x-',x)
print('i-',i)
# print("FullName-",FullName)
"""Since csv.writerows() takes a list but treats it as a row, we need to restructure our
data so that each row is one value from each list. This can be accomplished using zip():"""
with open("ram3.csv", "w") as outfile:
writer = csv.writer(outfile)
# Write headers
writer.writerow(data.keys())
# Make one row equal to one value from each list
rows = zip(*data.values())
# Write rows
writer.writerows(rows)
Note: I can't use pandas DataFrame
. That's why I have written code like this please tell me how to print my date/time column only date where i need to change in code to get that...thanks
Input:
job_Id Name Address Email Date/Time
1 snehil singh marathalli ss@gmail.com 12/10/2011:02:03:20
2 salman marathalli ss@gmail.com 12/11/2011:03:10:20
3 Amir HSR ar@gmail.com 11/02/2009:09:03:20
4 Rakhesh HSR rakesh@gmail.com 09/12/2010:02:03:55
5 Ram marathalli r@gmail.com 01/10/2014:12:03:20
6 Shyam BTM ss@gmail.com 12/11/2012:01:03:20
7 salman HSR ss@gmail.com 11/08/2016:15:03:20
8 Amir BTM ar@gmail.com 07/10/2013:04:02:30
9 snehil singh Majestic sne@gmail.com 03/03/2018:02:03:20
Csv file:
job_Id Name Address Email Date/Time
1 1 1 1 12/10/2011:02:03:20
2 2 1 1 12/11/2011:03:10:20
3 3 2 2 11/02/2009:09:03:20
4 4 2 3 09/12/2010:02:03:55
5 5 1 4 01/10/2014:12:03:20
6 6 3 1 12/11/2012:01:03:20
7 2 2 1 11/08/2016:15:03:20
8 3 3 2 07/10/2013:04:02:30
9 1 4 5 03/03/2018:02:03:20
In this output, everything is correct but only the date/time column. I want to print date only, and not time.
解决方案
if key=="Date/Time":
var="12/10/2011"
print(var)
var = data[key]
iter_obj1 = iter(var)
while True:
try:
element1 = next(iter_obj1)
date =element1[0:10]
print("date-",date)
except StopIteration:
break
我明白了,我不应该使用所有这些......只是添加了一行将在 for 循环中打印所需的输出..
if key=="Date/Time":
data[key][i] = data[key][i][0:10]
就是这样..它所做的一切都是一样的..
推荐阅读
- angular - 从 Angular 调用发布请求时缺少强制“grant_type”权限
- python - 如何获取两个集合的交集,然后使用“for循环”的一行代码从原始集合中删除这些元素?
- reactjs - 日视图,如何制作自定义活动卡片/活动内容?
- java - 在 Playstore (SQLiteConnection.java) 上启动后,我的 Flutter 应用程序在多个设备上崩溃
- ssis - 查找缓存模式 V/S 部分缓存 V/S 无缓存
- c# - 如何对齐图像中的水印
- tkinter - tkinter,存储用户条目以继续执行程序(初学者)
- javascript - 使用 useEffect() 钩子处理关键事件的正确方法是什么,另一方面触发本地状态更改?
- python - 无法从其他计算机访问 Django 开发服务器:无法访问此站点
- c++ - 有向图中的DFS遍历和循环