Python中文
首页
教程
问答
标签
搜索
登录
注册
在for循环中切换日期格式;Python
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我的数据中的日期以两种不同的格式存储:</p> <p><code>Dienstag 31. Dezember 2013</code>和<code>30. Juni 2007</code></p> <p>我编写脚本从这两种格式中提取<code>Year/Month/Day</code>,并将它们存储在一个列表中:</p> <pre><code>for row in reader: line_count = line_count + 1 if row[1] == "DATE": pass else: date = row[1].encode('utf-8') year = date.split('.')[1].split(" ")[2] day = date.split(" ")[0] day = day.replace('.', '') month = date.split('.')[1].split(' ')[1] </code></pre> <p>对于第一种格式</p> <p>以及</p> <pre><code>date = row[1].encode('utf-8') year = date.split('.')[1].split(" ")[2] day = date.split(" ")[0] day = day.replace('.', '') month = date.split('.')[1].split(' ')[1] </code></pre> <p>对于第二种格式</p> <p>然而,这些日期格式在整个数据集中随机出现(<code>row[1]</code>)。当Python遇到使用相应脚本的一种格式(比如<code>if</code>语句)时,有没有方法告诉Python? 谢谢。你知道吗</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>您可以检查字符串中的第一个字符是否为alpha。你知道吗</p> <pre><code>if date[0].isalpha(): # call your function for German dates here else: # call the other function </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
Python从点数组计算平均多边形
8 回答
python从点集创建计数网格
5 回答
python从父对象实例化子实例
10 回答
python从父目录“\uu init\uu.py”fi导入
3 回答
python从父目录导入.json文件
5 回答
Python从父目录导入并保持flake8愉快
7 回答
Python从父目录导入模块,Flask Unittest示例
7 回答
Python从父目录相对导入
7 回答
Python从父目录运行子进程
1 回答
Python从父类继承变量
5 回答
Python从父级相对导入搜索路径
6 回答
Python从父线程/主线程与子线程的多线程交互
5 回答
Python从父静态方法调用子静态变量
5 回答
Python从特定lin写入文件
7 回答
Python从特定ord中的列表中检索值
4 回答
Python从特定ord中的列表调用函数
6 回答
Python从特定位置的列表中减去一个数字
4 回答
python从特定位置读取二进制文件
1 回答
Python从特定函数捕获警告消息
7 回答
Python从特定列写入csv numb
6 回答