How to automatically save data in different Excel by using with open statement under Python?

when writing a crawler program, there are multiple contents in the same directory. How to use with open river data to automatically save in different Excel?

for example, I want to use with open to create different tables, such as Beijing .xls; Shanghai .xls; Guangzhou .xls; and so on
, but I can"t implement

at present.

the code is as follows

global city
for city in ("","",""):
    path=str(r"C:\Users\Batman\Desktop\city.csv")
    with open(path,"a",encoding="ANSI") as f:
        f.close()
        

but you can only get one city.csv
is there any solution?

Mar.14,2021

just use Excel to open * *. Csv , but you may need to set a delimiter. The easiest thing to do is to separate the column from the column with a space

.

or use xlrd library to modify Excel

.

the pandas library is recommended, which is much more convenient than xlrd or xlwt , and can operate on different data sources with the same interface.

here is an example of ipython

In [1]: import pandas as pd

In [2]: df = pd.read_csv('a.csv', encoding='utf-8')

In [3]: df
Out[3]: 
      
0    10
1    11
2    12
3    20
4    21
5    22

In [4]: df[df[u''].eq(u'')].to_excel('.xls', encoding='utf-8', index=False)

In [5]: df_gz = pd.read_excel('.xls', encoding='utf-8')

In [6]: df_gz
Out[6]: 
      
0    10
1    11
2    12
Menu