python CSV writer, pandas , How to write the csv file in two column using the same text file has only one coulmn

huangapple go评论89阅读模式
英文:

python CSV writer, pandas , How to write the csv file in two column using the same text file has only one coulmn

问题

  1. 假设文本文件包含以下元素
  2. A
  3. B
  4. C
  5. D
  6. 我想使用Python将这些元素写入CSV文件如下所示
  7. A,A
  8. A,B
  9. A,C
  10. A,D
  11. B,A
  12. B,B
  13. B,C
  14. B,D
  15. ...
  16. 我尝试使用以下代码将文本写入带有复制数据的CSV
  17. ```python
  18. import csv
  19. with open(r'C:\Users\soso-\Desktop\SVM\DataSet\drug_list.txt') as f:
  20. with open('integrated_x.csv', 'w') as out_file:
  21. for line in f:
  22. for x in range(548):
  23. out_file.write(f"{line.strip()}\n")
  24. f.close()
  25. out_file.close()

输出:

  1. A
  2. A
  3. A
  4. A
  5. B
  6. B
  7. B
  8. ...

然后在另一个CSV文件中为CSV文件添加新列,如下所示:

  1. with open(r'C:\Users\soso-\Desktop\SVM\DataSet\drug_list.txt') as f:
  2. with open(r'integrated_x.csv') as read_obj, \
  3. open('integrated_x12.csv', 'w', newline='') as write_obj:
  4. # 从输入文件对象创建csv.reader对象
  5. csv_reader = csv.reader(read_obj)
  6. # 从输出文件对象创建csv.writer对象
  7. csv_writer = csv.writer(write_obj)
  8. # 将输入csv文件的每一行读取为列表
  9. for row in csv_reader:
  10. for line in f:
  11. # 将默认文本附加到行/列表中
  12. row.append(line.strip())
  13. # 将更新后的行/列表添加到输出文件
  14. csv_writer.writerow(row)

输出:

  1. A,"A"
  2. A,"A,B"
  3. A,"A,B,C"

所以输出不如预期,它将列附加为列表并复制上一行。如何修复它?

  1. <details>
  2. <summary>英文:</summary>
  3. suppose the text file contain these elements as follows:
  4. A
  5. B
  6. C
  7. D
  8. I want to write these elements in the CSV file using python as follows:
  9. A,A
  10. A,B
  11. A,C
  12. A,D
  13. B,A
  14. B,B
  15. B,C
  16. B,D
  17. ...
  18. I try to write the text in csv with multiplicated data like this:
  19. import csv
  20. with open(r&#39;C:\Users\soso-\Desktop\SVM\DataSet\drug_list.txt&#39;) as f:
  21. with open(&#39;integrated_x.csv&#39;, &#39;w&#39;) as out_file:
  22. for line in f:
  23. for x in range(548):
  24. out_file.write(f&quot;{line.strip()}\n&quot;)
  25. f.close()
  26. out_file.close()
  27. output:
  28. A
  29. A
  30. A
  31. A
  32. B
  33. B
  34. B
  35. ....
  36. Then add the new column for the csv file in the other csv file like this:
  37. with open(r&#39;C:\Users\soso-\Desktop\SVM\DataSet\drug_list.txt&#39;) as f:
  38. with open(r&#39;integrated_x.csv&#39;) as read_obj, \
  39. open(&#39;integrated_x12.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as write_obj:
  40. # Create a csv.reader object from the input file object
  41. csv_reader = csv.reader(read_obj)
  42. # Create a csv.writer object from the output file object
  43. csv_writer = csv.writer(write_obj)
  44. # Read each row of the input csv file as list
  45. for row in csv_reader:
  46. for line in f:
  47. # Append the default text in the row / list
  48. row.append(line)
  49. # Add the updated row / list to the output file
  50. csv_writer.writerow(row)
  51. output:
  52. A,&quot;A&quot;
  53. A,&quot;A,B&quot;
  54. A,&quot;A,B,C&quot;
  55. So the output not as expected it append the column as list and replicate the previous row.
  56. How can I fix it?
  57. </details>
  58. # 答案1
  59. **得分**: 0
  60. 使用标准库中的itertools.permutations:
  61. ```python
  62. import itertools
  63. list(itertools.permutations(['A', 'B', 'C', 'D'], 2))

如果要将其保存为CSV文件,可以使用pandas:

  1. import pandas as pd
  2. import itertools
  3. a = list(itertools.permutations(['A', 'B', 'C', 'D'], 2))
  4. df = pd.DataFrame(list(itertools.permutations(['A', 'B', 'C', 'D'], 2)))
  5. df.to_csv('my_file.csv')

这是创建这种排列的最简单方式。

英文:

Use itertools.permutations from the standard library:

  1. import itertools
  2. list(itertools.permutations([&#39;A&#39;, &#39;B&#39;, &#39;C&#39; ,&#39;D&#39;], 2))
  3. [(&#39;A&#39;, &#39;B&#39;), (&#39;A&#39;, &#39;C&#39;), (&#39;A&#39;, &#39;D&#39;), (&#39;B&#39;, &#39;A&#39;), (&#39;B&#39;, &#39;C&#39;), (&#39;B&#39;, &#39;D&#39;), (&#39;C&#39;, &#39;A&#39;), (&#39;C&#39;, &#39;B&#39;), (&#39;C&#39;, &#39;D&#39;), (&#39;D&#39;, &#39;A&#39;), (&#39;D&#39;, &#39;B&#39;), (&#39;D&#39;, &#39;C&#39;)]

If you want to save it as a csv file, then by pandas:

  1. &gt;&gt;&gt; import pandas as pd
  2. &gt;&gt;&gt; import itertools
  3. &gt;&gt;&gt; a = list(itertools.permutations([&#39;A&#39;, &#39;B&#39;, &#39;C&#39; ,&#39;D&#39;], 2))
  4. &gt;&gt;&gt; df = pd.DataFrame(list(itertools.permutations([&#39;A&#39;, &#39;B&#39;, &#39;C&#39; ,&#39;D&#39;], 2)))
  5. &gt;&gt;&gt; df
  6. 0 1
  7. 0 A B
  8. 1 A C
  9. 2 A D
  10. 3 B A
  11. 4 B C
  12. 5 B D
  13. 6 C A
  14. 7 C B
  15. 8 C D
  16. 9 D A
  17. 10 D B
  18. 11 D C
  19. &gt;&gt;&gt; df.to_csv(&#39;my_file.csv&#39;)

This is easiest way to create this permutation

huangapple
  • 本文由 发表于 2023年2月8日 17:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定