鱼C论坛

 找回密码
 立即注册
查看: 830|回复: 2

[已解决]python读取文件列时遇到字符“”

[复制链接]
发表于 2020-1-6 10:58:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 su咩咩 于 2020-1-6 14:33 编辑

读取文件中评论时可以读取出内容。但是评论中有“,”时就停止读取直接跳到下一行了,若没有逗号则可以读取完整句话。代码如下:

  1. # -*- coding=utf-8 -*-
  2. import numpy as np
  3. p = r'E:\code\12.27 convert text\test.csv'
  4. data = np.loadtxt(p,str,delimiter = ",",usecols = (0,1,5))#usecols参数介绍一个元组,元组里面用列索引来指定输入特定的列。
  5. print(data[:])
复制代码

运行结果(标注的位置是遇到“,”的情况,文本没有读取完):
[['{"reviewerID": "A1KLRMWW2FWPL4"' ' "asin": "0000031887"'
  ' "reviewText": "This is a great tutu and at a really great price. It doesn\'t look cheap at all. I\'m so glad I looked on Amazon and found such an affordable tutu that isn\'t made poorly. A++"']
['{"reviewerID": "A2G5TCU2WDFZ65"' ' "asin": "0000031887"'
  ' "reviewText": "I bought this for my 4 yr old daughter for dance class']
['{"reviewerID": "A1RLQXYNCMWRWN"' ' "asin": "0000031887"'
  ' "reviewText": "What can I say... my daughters have it in orange']
要读取的文件:
{"reviewerID": "A1KLRMWW2FWPL4", "asin": "0000031887", "reviewerName": "Amazon Customer \"cameramom\"", "helpful": [0, 0], "reviewText": "This is a great tutu and at a really great price. It doesn't look cheap at all. I'm so glad I looked on Amazon and found such an affordable tutu that isn't made poorly. A++", "overall": 5.0, "summary": "Great tutu-  not cheaply made", "unixReviewTime": 1297468800, "reviewTime": "02 12, 2011"}
{"reviewerID": "A2G5TCU2WDFZ65", "asin": "0000031887", "reviewerName": "Amazon Customer", "helpful": [0, 0], "reviewText": "I bought this for my 4 yr old daughter for dance class, she wore it today for the first time and the teacher thought it was adorable. I bought this to go with a light blue long sleeve leotard and was happy the colors matched up great. Price was very good too since some of these go for over $15.00 dollars.", "overall": 5.0, "summary": "Very Cute!!", "unixReviewTime": 1358553600, "reviewTime": "01 19, 2013"}
{"reviewerID": "A1RLQXYNCMWRWN", "asin": "0000031887", "reviewerName": "Carola", "helpful": [0, 0], "reviewText": "What can I say... my daughters have it in orange, black, white and pink and I am thinking to buy for they the fuccia one. It is a very good way for exalt a dancer outfit: great colors, comfortable, looks great, easy to wear, durables and little girls love it. I think it is a great buy for costumer and play too.", "overall": 5.0, "summary": "I have buy more than one", "unixReviewTime": 1357257600, "reviewTime": "01 4, 2013"}
{"reviewerID": "A8U3FAMSJVHS5", "asin": "0000031887", "reviewerName": "Caromcg", "helpful": [0, 0], "reviewText": "We bought several tutus at once, and they are got high reviews. Sturdy and seemingly well-made. The girls have been wearing them regularly, including out to play, and the tutus have stood up well. Fits the 3-yr old & the 5-yr old well. Clearly plenty of room to grow. Only con is that when the kids pull off the tutus, the waste band gets twisted, and an adult has to un-tangle. But this is not difficult.", "overall": 5.0, "summary": "Adorable, Sturdy", "unixReviewTime": 1398556800, "reviewTime": "04 27, 2014"}
{"reviewerID": "A3GEOILWLK86XM", "asin": "0000031887", "reviewerName": "CJ", "helpful": [0, 0], "reviewText": "Thank you Halo Heaven great product for Little Girls.  My Great Grand Daughters Love these Tutu's.  Will buy more from this seller.  Made well and cute on the girls.  Thanks for a great product.NEVER BUY FROM DRESS UP DREAMS........I will buy more as long as I don't buy from "Dress Up Dreams"  I never rec'd or order in FL. Only rec'd pink, the purple one was missing.  Company is a rip off.  REFUSES to make good on purchase...... Real creeps.", "overall": 5.0, "summary": "Grammy's Angels Love it", "unixReviewTime": 1394841600, "reviewTime": "03 15, 2014"}
{"reviewerID": "A27UF1MSF3DB2", "asin": "0000031887", "reviewerName": "C-Lo \"Cynthia\"", "helpful": [0, 0], "reviewText": "I received this today and I'm not a fan of it but my daughter is I thought it would be puffier as it looks in the pic but it's not and the one they sent me is pink underneath and the waist band is pink which is not what I wanted due to the fact she already had the sandals she was gonna wear with it now I gotta find another pair of sandals,ima just keep it cuz she likes it.", "overall": 4.0, "summary": "It's ok", "unixReviewTime": 1396224000, "reviewTime": "03 31, 2014"}
{"reviewerID": "A16GFPNVF4Y816", "asin": "0000031887", "reviewerName": "design maven", "helpful": [0, 0], "reviewText": "Bought this as a backup to the regular ballet outfit my daughter has to wear. So far, she's using it to play out her Cinderella dreams but I am sure we'll be able to use it for a recital sometime soon. The quality is just fine for the price we paid. I was not expecting a designer skirt for this price and got exactly what I paid for.", "overall": 5.0, "summary": "Great for dress-up and for ballet practice", "unixReviewTime": 1399075200, "reviewTime": "05 3, 2014"}
{"reviewerID": "A2M2APVYIB2U6K", "asin": "0000031887", "reviewerName": "Jamie P.", "helpful": [0, 0], "reviewText": "Great tutu for a great price. It isn't a "full" or high quality skirt, but it is perfect for my daughter to wear over leggings for her little outfits.", "overall": 5.0, "summary": "Great value", "unixReviewTime": 1356220800, "reviewTime": "12 23, 2012"}
{"reviewerID": "A1NJ71X3YPQNQ9", "asin": "0000031887", "reviewerName": "JBerger", "helpful": [0, 0], "reviewText": "My daughter liked this, and it with her costume, but she would have liked it to be a bit fuller.", "overall": 4.0, "summary": "Good", "unixReviewTime": 1384041600, "reviewTime": "11 10, 2013"}
{"reviewerID": "A3EERSWHAI6SO", "asin": "0000031887", "reviewerName": "Jeffrey Hollingshead \"Jillian hollingshead\"", "helpful": [7, 8], "reviewText": "For what I paid for two tutus is unbeatable anywhere!  I ordered a pink and turquios and they are vibrant and beautiful! The tutu is very full! Princess style! Not cheaply made! Not cheap materia! Obviously someone made these with love and care! I paid less than 7 bucks for a tutu I and I feel proud of my self for researching to the point of finding gold!Recommend 2-6 years!My daughter is two ! Wears size 4t and this skirt ( one size ) fit perfect and will probaly be able to accommodate her quickly growing waist for some time!", "overall": 5.0, "summary": "WOW !! ..is all I have to say!", "unixReviewTime": 1349568000, "reviewTime": "10 7, 2012"}
最佳答案
2020-1-6 21:59:34
希望可以帮到你

  1. def c(s=None):
  2.     with open("s.csv") as f:
  3.         f_csv = csv.reader(f)
  4.         head = next(f_csv)
  5.         for now in f_csv:
  6.             print(now)
  7.             # now 是一个list数据,即一行数据

  8. def d(s=None):
  9.     with open("s.csv") as f:
  10.         f_csv = csv.DictReader(f)
  11.         for row in f_csv:
  12.             print(row)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-1-6 21:59:34 | 显示全部楼层    本楼为最佳答案   
希望可以帮到你

  1. def c(s=None):
  2.     with open("s.csv") as f:
  3.         f_csv = csv.reader(f)
  4.         head = next(f_csv)
  5.         for now in f_csv:
  6.             print(now)
  7.             # now 是一个list数据,即一行数据

  8. def d(s=None):
  9.     with open("s.csv") as f:
  10.         f_csv = csv.DictReader(f)
  11.         for row in f_csv:
  12.             print(row)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-1-7 19:48:46 | 显示全部楼层
谢谢~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-26 18:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表