简体   繁体   English

如何从CSV文件中读取包含逗号的列表作为列?

[英]How to read list which contains comma from CSV file as a column?

I want to read CSV file which contains following data :我想读取包含以下数据的 CSV 文件:

Input.csv-输入.csv-

 10,[40000,1][50000,5][60000,14]
 20,[40000,5][50000,2][60000,1][70000,1][80000,1][90000,1]
 30,[60000,4]
 40,[40000,5][50000,14]

I want to parse this CSV file and parse it row by row.我想解析这个 CSV 文件并逐行解析它。 But these lists contains commas ',' so I'm not getting correct result.但是这些列表包含逗号','所以我没有得到正确的结果。

Program-Code-程序代码-

if __name__ == "__main__":

    with open(inputfile, "r") as f:
        reader = csv.reader(f,skipinitialspace=True)
        next(reader,None)
        for read in reader:
            no = read[0]
            splitted_record = read[1]          
            print splitted_record

Output-输出-

[40000
[40000
[60000
[40000

I can understand read.csv method reads till commas for each column.我可以理解read.csv方法读取每列的逗号。 But how I can read whole lists as a one column?但是我如何将整个列表作为一列阅读?

Expected Output-预期输出-

[40000,1][50000,5][60000,14]
[40000,5][50000,2][60000,1][70000,1][80000,1][90000,1]
[60000,4]
[40000,5][50000,14]

Writing stuff to other file-将内容写入其他文件-

name_list = ['no','splitted_record']
file_name = 'temp/'+ no +'.csv'
if not os.path.exists(file_name):
    f = open(file_name, 'a')
    writer = csv.DictWriter(f,delimiter=',',fieldnames=name_list)
    writer.writeheader()
else:
    f = open(file_name, 'a')
    writer = csv.DictWriter(f,delimiter=',',fieldnames=name_list)
writer.writerow({'no':no,'splitted_record':splitted_record})

How I can write this splitted_record without quote "" ?我怎样才能写这个splitted_record没有引号""

you can join those items together, since you know it split by comma您可以将这些项目连接在一起,因为您知道它用逗号分隔

if __name__ == "__main__":

    with open(inputfile, "r") as f:
        reader = csv.reader(f,skipinitialspace=True)
        next(reader,None)
        for read in reader:
            no = read[0]
            splitted_record = ','.join(read[1:])          
            print splitted_record

output输出

[40000,1][50000,5][60000,14]
[40000,5][50000,2][60000,1][70000,1][80000,1][90000,1]
[60000,4]
[40000,5][50000,14]

---update--- data is the above output ---更新---数据是上面的输出

with open(filepath,'wb') as f:
     w = csv.writer(f)
     for line in data:
         w.writerow([line])

You can use your own dialect and register it to read as you need.您可以使用自己的方言并将其注册为您需要的阅读。 https://docs.python.org/2/library/csv.html https://docs.python.org/2/library/csv.html

暂无
暂无

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

相关问题 如何使用python在csv文件中写入包含逗号的列表? - How to write list which contains comma in csv file using python? 如果列中包含逗号分隔的元素,如何将逗号分隔的 `csv` 文件读入 pandas dataframe? - How to read a comma separated `csv` file into pandas dataframe if it contains a comma separated elements in a column? Python:读取其中一列包含多个逗号的csv文件 - Python: Read csv file of which one column contains multiple commas 在python中读取包含列表和逗号分隔的文件 - read a file contains list and comma separation in python 如何从csv文件制作直方图,该文件在python中只包含一列数字? - How do I make a histogram from a csv file which contains a single column of numbers in python? 当列标题包含正斜杠时,如何从 CSV 文件中读取数据? - How to read data from a CSV file when the column heading contains a forward slash? 如果路径名包含单个反逗号并且出现错误,如何在 pandas 中读取 csv 文件? - How to read csv file in pandas if the pathname contains single inverted comma and i get error? 如何从python中的csv文件读取列 - How to read a column from a csv file in python 如何从csv文件中读取某一列? - How to read a certain column from csv file? 我的数据在列的值中有逗号,这也是一个分隔符,如何通过 csv.reader 在 python 中读取它 - My data has comma in the value of the column which is also a delimiter, how to read it by csv.reader in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM