How to make strings into a list?

MH Yen

I try to edit one of the column data into the format I want by using csv.DictReader(), the code is like this:

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_)

the outcome is like this:

20170103
20170104
20170105
20170106
20170109
20170110
20170111
20170112
20170113
20170116
20170117
20170118
20170119
20170120
20170123
20170124

But I want the data in the way like this:

date_list = ['20170103', '20170104', '20170105', '20170106', '20170109', '20170110', '20170111', '20170112', '20170113', '20170116', '20170117', '20170118', '20170119', '20170120', '20170123', '20170124']
mhawke

You could write it as a generator. This gives you the ability to iterate over the sequence of dates from the file, or collect them together in a list. I have simplified your code :

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))

Usage:

# 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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to make a list of strings with a for loop

How to make a List[String] to be a list of strings

How to make a list with a comma separated strings?

How to make a histogram from a list of strings in Python?

How to call a List with Strings to make the call dynamic

how to make list of strings from file where this strings are in own line

How to make a list of numbers out of strings in a list of lists in Python

How to remove select characters from a list of strings, and make a list of strings exactly 4 characters long

How to make a deep copy of a list of strings in emacs lisp?

How can I make sublists from a list based on strings in python?

How to make mysql like and not like fetch strings from a list

Python: How do you make a list start with the lowercase strings?

How to make sure that my list comprehension returns a list of strings that are not present in a different list?

How do I make a list of strings withouth invalid characters in any of the strings?

How to make a combination for strings?

Python/ Openpyxl: How to pass a list to an if statement and make the cells bold if they contain any strings from the list value (MacOS)

How to sort a list of strings?

How to format a list of strings into

How to add strings to a list?

How to replace strings in a List

How to edit strings in a list

How to serialize a list of strings

How to filter a list of strings?

How to manipulate strings in a list?

Make changes to a list of strings within a for loop

Scala make List of strings from char array

How to make a function that compares strings?

How to make a strings input not null

How to make a multiline title with strings