简体   繁体   English

如何从 csv 文件中删除读取值中的双引号

[英]How to remove double quotes in value reading from csv file

My csv file:我的 csv 文件:

Mp4,Mp3,"1234554"

My code:我的代码:

csv=csv=b''.join(csv).split(b'\n')
for index,row in enumerate(csv):
  row=re.split(b''',(?=(?:[^'"]|'[^']*'|"[^"]*")*$)''',row)
  for records in row:
      print(records)

when it printing the records,for the 3rd element it prints with ""i need to ignore this doubles quotes.当它打印记录时,对于它打印的第三个元素“”我需要忽略这个双引号。

I think this should do it:我认为应该这样做:

records = records.replace("\"","")

Edit编辑


Using pandas.read_csv is better for working with csv files使用pandas.read_csv更适合使用 csv 文件

import pandas as pd
csv = pd.read_csv('data.csv', delimiter=',', names=['x', 'y', 'z'])

# iterate over the dataframe
for index, row in csv.iterrows():
    print(row['x'], row['y'], row['z'])

Assuming content of data.csv looks like假设data.csv的内容看起来像

Mp4,Mp3,"1234554"

The Output would look like this: Output 看起来像这样:

Mp4 Mp3 1234554

If your csv file includes column names eg如果您的 csv 文件包含列名,例如

file_type1,file_type2,size
mp4,mp3,"1234554"

Just remove the names parameter if you read in the csv file:如果您在 csv 文件中阅读,只需删除names参数:

csv = pd.read_csv('data.csv', delimiter=',')
print(csv)

Then the Output would look like this:然后 Output 看起来像这样:

  file_type1 file_type2     size
0        mp4        mp3  1234554

Read more about pandas or pandas.read_csv阅读更多关于pandaspandas.read_csv

You could easlily replace it with您可以轻松地将其替换为

print(records.replace('"',''))

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

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