Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使字符串成为列表?_Python_Python 3.x_Csv - Fatal编程技术网

Python 如何使字符串成为列表?

Python 如何使字符串成为列表?,python,python-3.x,csv,Python,Python 3.x,Csv,我尝试使用csv.DictReader()将其中一个列数据编辑为我想要的格式,代码如下: import csv import time with open('201701.csv') as inf: reader = csv.DictReader(inf) for row in reader: date = row['日期'] d = date.split('/') year = int(d[

我尝试使用
csv.DictReader()
将其中一个列数据编辑为我想要的格式,代码如下:

import csv
import time
with open('201701.csv') as inf:
        reader = csv.DictReader(inf)
        for row in reader:
            date = row['日期']
            d = date.split('/')
            year = int(d[0]) + 1911
            month = str(d[1])
            day = str(d[2])
            date_ = str(year) + "{0:2}".format(month) + "{0:2}".format(day)
            print(date_)
date_list = ['20170103', '20170104', '20170105', '20170106', '20170109', '20170110', '20170111', '20170112', '20170113', '20170116', '20170117', '20170118', '20170119', '20170120', '20170123', '20170124']
结果是这样的:

20170103
20170104
20170105
20170106
20170109
20170110
20170111
20170112
20170113
20170116
20170117
20170118
20170119
20170120
20170123
20170124
但我希望数据是这样的:

import csv
import time
with open('201701.csv') as inf:
        reader = csv.DictReader(inf)
        for row in reader:
            date = row['日期']
            d = date.split('/')
            year = int(d[0]) + 1911
            month = str(d[1])
            day = str(d[2])
            date_ = str(year) + "{0:2}".format(month) + "{0:2}".format(day)
            print(date_)
date_list = ['20170103', '20170104', '20170105', '20170106', '20170109', '20170110', '20170111', '20170112', '20170113', '20170116', '20170117', '20170118', '20170119', '20170120', '20170123', '20170124']

您的输出是这样的,因为您使用
print(date)
迭代地打印单个字符串日期。这根本不是一份清单

要制作列表,请将日期字符串附加到列表
date\u list

import csv
import time
date_list = []    # create list of date
with open('201701.csv') as inf:with open('201701.csv') as inf:
        reader = csv.DictReader(inf)
        for row in reader:
            date = row['日期']
            d = date.split('/')
            year = int(d[0]) + 1911
            month = str(d[1])
            day = str(d[2])
            date_ = str(year) + "{0:2}".format(month) + "{0:2}".format(day)
            date_list.append(date_)  # append your date string to list
print(date_list)

嗯,如果你想要一个日期列表,那么你应该做一个

import csv
import time
date_list = []
with open('201701.csv') as inf:with open('201701.csv') as inf:
    reader = csv.DictReader(inf)
    for row in reader:
        date = row['日期']
        d = date.split('/')
        year = int(d[0]) + 1911
        month = str(d[1])
        day = str(d[2])
        date_ = str(year) + "{0:2}".format(month) + "{0:2}".format(day)
        print(date_)
        date_list.append(date_)
print(date_list)

你可以把它写成发电机。这使您能够遍历文件中的日期序列,或者将它们收集到一个列表中。我简化了您的代码:

import csv
import time

def get_dates(inf):
    for row in csv.DictReader(inf):
        year, month, day = row['日期'].split('/')
        yield '{}{:02}{:02}'.format(int(year) + 1911, int(month), int(day))
用法:

# Collect into a list
with open('201701.csv') as inf:
    date_list = list(get_dates(inf))
    print(date_list)

# Or iterate over the dates
with open('201701.csv') as inf:
    for date in get_dates(inf):
        print(date)