Python-修改一个csv文件
现在,我知道在读取csv文件时通常不可行,因此您需要创建一个新的csv文件并对其进行写入.我遇到的问题是保留数据的原始顺序.
Now I know it's usually not feasible to modify a csv file as you are reading from it so you need to create a new csv file and write to it. The problem I'm having is preserving the original order of the data.
输入的csv文件如下所示:
The input csv file looks like follows:
C1 C2 C3
apple BANANA Mango
pear PineApple StRaWbeRRy
我想将所有数据转换为小写并输出一个新的csv文件,如下所示:
I want to turn all the data into lower case and output a new csv file that looks like:
C1 C2 C3
apple banana mango
pear pineapple strawberry
到目前为止,我可以遍历输入的csv文件并将所有值都转换为小写,但是我不知道如何将其重新写成该格式的csv文件.我的代码是:
So far I can iterate through the input csv file and turn all the values into lower case but I don't know how to rewrite it back into a csv file in that format. The code I have is:
def clean (input)
aList = []
file = open(input, "r")
reader = csv.reader(file, delimiter = ',')
next(reader, None) # Skip the header but I want to preserve it in the output csv file
for row in reader:
for col in row:
aList.append(col.lower())
所以现在我有了一个包含所有小写数据的列表,如何将其重写回与输入相同格式(行和列数相同)的csv文件中,包括我在代码中跳过的标题行
So now I have a list with all the lowercase data, how do I rewrite it back into a csv file of the same format (same number of rows and columns) as the input including the header row that I skipped in the code.
熊猫方式:
使用 pandas 读取文件并获取数据框.然后,您可以简单地使用 lower()
Read the file using pandas and get the dataframe. Then you can simply use lower()
import pandas as pd
def conversion(text):
return text.lower()
df = pd.read_csv(file_path)
df[column_name] = df[column_name].map(conversion)
甚至是一个班轮:
df[column_name] = df[column_name].apply(lambda x: x.lower()) # If you have nan or other non-string values, you may need to convert x to string first like str(x).lower()
然后,您可以使用 to_csv 函数