简体   繁体   English

Python:csv_writer 在单独的单元格中写入 1 个数字问题

[英]Python: csv_writer writing 1 number in separate cells problem

I have built a simple webscraper that gets the population from a website given the city name.我已经构建了一个简单的网络爬虫,可以从给定城市名称的网站获取人口。 The output is in the string format: "City, State | Data USA Population" output 采用字符串格式:“City, State | Data USA Population”

For example, running print(search.text) for New Smyrna Beach it returns the string:例如,为 New Smyrna Beach 运行 print(search.text) 会返回字符串:

New Smyrna Beach, FL |佛罗里达州新士麦那海滩 | Data USA 25,770数据 美国 25,770

I want the program to write this in a csv file but the number is being separated in multiple columns when I would like it in one cell.我希望程序将其写在 csv 文件中,但是当我希望在一个单元格中时,该数字被分成多列。 It appears the "New Smyrna Beach, FL | Data USA" is not being writed at all, how come?似乎根本没有写“新士麦那海滩,佛罗里达州 | Data USA”,为什么?

Here is my code for the csv writing:这是我编写 csv 的代码:

import csv
with open('population.csv', 'w') as f:
   thewriter = csv.writer(f)
   thewriter.writerow(search.text)

And here is my output in the CSV file.这是我在 CSV 文件中的 output。

2| 2| 5|,| 5|,| 7| 7| 7| 7| 0| 0| (each | denotes a new column) I want the csv file to write 25,770 in one cell just like that. (每个 | 表示一个新列)我希望 csv 文件像这样在一个单元格中写入 25,770。 Also not required but would be useful if the program actually wrote the city name as well.也不是必需的,但如果程序实际上也写了城市名称,这将很有用。 What did I do wrong?我做错了什么?

Given that you can't split on , or鉴于您不能拆分, due to your population containing a comma and your city name containing spaces.由于您的人口包含逗号和您的城市名称包含空格。 We could do this a few ways.我们可以通过几种方式做到这一点。

  1. Perform two splits on both , and对两者执行两次拆分,并且 and returning the correct position with a list slice并返回带有列表slice的正确 position
  2. Use regex to extract the information使用正则表达式提取信息

Splitting分裂

search = 'New Smyrna Beach, FL | Data USA 25,770'
with open('test.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerow([search.split(',')[0], search.split(' ')[-1]])

Regex正则表达式

import re

with open('test.csv', 'w') as f:
    writer = csv.writer(f)
    city = re.match(r'^([^,]*)', search).group(0)
    population = re.findall(r'(\d+,\d+)', search)[0]

    writer.writerow([city, population])

Output Output

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM