I want Python to extract specific information from csv files

Asked 1 years ago, Updated 1 years ago, 288 views

I've rewritten this question a lot, so it's okay now.
Next question <
I want to count the number of cities by province from the csv file

Using Python, we would like to extract prefectures and municipalities from the data of prefectural districts (see image 1).

However, I got an error in image 2 on Python, but I don't know how to solve it.

Enter a description of the image here

Enter a description of the image here

python regular-expression

2022-12-28 23:42

2 Answers

Here is a brief example of re.search.

import re

s=["Tokyo", "Hokkaido", "Nagasaki Prefecture", "Kyoto Prefecture", "Nagoya City"]
for iins:
    print(re.search("City | Road | Prefectural | Prefectural", i))
# <re. Match object; span=(2,3), match='City'>
# <re. Match object; span=(2,3), match='road'>
# <re. Match object; span=(2,3), match='Prefect'>
# <re. Match object; span=(1,2), match='City'>
# None

Please enclose the regular expression with "


2022-12-31 10:04

I want to extract prefectures and municipalities from the data of prefectural districts.

KEN_ALL.CSV may have been downloaded from Japan Postcode Data Download The kana data's prompt and sulky sounds are written in small letters from State and city names are listed in columns 7 and 8 respectively, so you will have to extract them.

import csv

filename = 'KEN_ALL.CSV'
prefs, cities = [ ], [ ]
with open(filename, encoding='Shift JIS', newline=')asf:
    csvreader=csv.reader(f)
    for row in csvreader:
        prefs.append(row[6])#7th column: State/Province name
        cities.append(row[7])#8th column: City name

# Remove Duplicate
prefix=[*{*prefs}]
cities=[*{*cities}]

print(prefs)#47 cases
# print(cities)#1894 cases


2022-12-31 20:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.